preg_match find multiple partial appearances of a word in a string

Posted by Rubytastic on Stack Overflow See other posts from Stack Overflow or by Rubytastic
Published on 2012-06-20T15:06:58Z Indexed on 2012/06/20 15:16 UTC
Read the original article Hit count: 238

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about count