php - Need to match ALL similar words/phrases using preg_match_all -
i'm trying create pattern matches all similar words/phrases within string.
for example, need match: "this", "this is", "this it", "that", "that was", "that not".
it matches first occurence of "this", should match all occurences.
i tried anchors , word boundaries, nil seems work.
i tried (simplified):
$content = "this it! not!"; preg_match_all('/(this|this is|this it|that|that was|that not)/i', $content, $results);
which should output:
this this is this it that that was that not
given you're capturing terms you're searching for, might improve utilize foreach
loop substr_count
see how many times each string occurs.
for example:
class="lang-php prettyprint-override">$haystack = "this it! not! not test!"; $needles = array( "this", "this is", "this it", "that", "that was", "that not"); foreach ($needles $needle) { // substr_count case sensitive, create subject , search lowercase $hits = substr_count(strtolower($haystack), strtolower($needle)); echo "search '$needle' occurs $hits time(s)" . php_eol; }
the above output:
class="lang-none prettyprint-override">search 'this' occurs 2 time(s) search 'this is' occurs 2 time(s) search 'this it' occurs 1 time(s) search 'that' occurs 1 time(s) search 'that was' occurs 1 time(s) search 'that not' occurs 1 time(s)
if substr_count
doesn't provide flexibility need can replace preg_match_all
, utilize individual $needle
values search terms.
php regex web preg-match preg-match-all
No comments:
Post a Comment