Skip to content

PHP – Search Words & Phrases


Warning: Use of undefined constant gad_content_tag_filter_replace - assumed 'gad_content_tag_filter_replace' (this will throw an Error in a future version of PHP) in /home/surfer_u/labs.andrewu.net/wp-content/plugins/web-ninja-google-analytics/webninja_ga.php on line 1813

<?php
$search_expression = "apple bear \"Tom Cruise\" or 'Mickey Mouse' another word";
$words = preg_split("/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|" . "[\s,]*'([^']+)'[\s,]*|" . "[\s,]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY |PREG_SPLIT_DELIM_CAPTURE);
print_r($words);
?>

The result will be:
Array
(
[0] => apple
[1] => bear
[2] => Tom Cruise
[3] => or
[4] => Mickey Mouse
[5] => another
[6] => word
)

1. Accepted delimiters: white spaces (space, tab, new line etc.) and commas.

2. You can use either simple (‘) or double (“) quotes for expressions which contains more than one word.

source

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*