I have these following variables:
$texttofind = "story of a test";
$paragraph = "the story of a test and a test paragraph used for
testing the story of a test paragraph";
I would like to use preg_match();
to return the count, which in this case would be 2. any ideas?
There is no need for regular expressions, simply use substr_count
:
echo 'Found texttofind ' . substr_count($paragraph, $texttofind) . ' times';
If you really want to use regular expressions, you can use the following (slower, and unnecessarily complex) expression:
echo preg_match_all('/' . preg_quote($texttofind, '/') . '/', $paragraph);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.