Any regex gurus around here? its driving me crazy.
Say I have this string:
“bookstore books Booking”
I want to count the number “books” appears in this and return the number.
Currently I have this which is not working:
$string = "bookstore books Booking";
if (preg_match_all('/\b[A-Z]+books\b/', $string, $matches)) {
echo count($matches[0]) . " matches found";
} else {
echo "match NOT found";
}
On top of this the “books” inside the preg_match_all should become a $var
Anyone an idea how to count correctly?
It’s actually much simpler, you can use preg_match_all() like this:
Or use the function that was made for this purpose, substr_count():