![]() |
Extracting words from a given string is a common task in PHP, often used in text processing and manipulation. Below are the approaches to extract words from a given string in PHP: Table of Content Using explode() functionIn this approach, we are using the explode() function in PHP which is used to split a string into an array based on a specified delimiter. Here we use a space character as the delimiter to split the string into words. Syntax:words = explode(delimiter, string); Example: This example uses explode() function to extract words from given string.
Output Array ( [0] => Welcome [1] => to [2] => GeeksForGeeks ) Using regular expressionsIn this approach, we are using regular expressions to extract words from a string. We use the preg_match_all() function with a regex pattern to find all words in a string. Syntax:preg_match_all(pattern, string, matches); Example: This example uses regular expressions to extract words from given string.
Output Array ( [0] => Hello [1] => Geeks ) |
Reffered: https://www.geeksforgeeks.org
PHP |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |