简体   繁体   中英

Find the occurences of a substring in string using php?

I need to count the number of occurences of the substring in a string.

my code like,

$str1 = "when he arrives, he want to open the door.";
$find_str = "he";

here the string "he" is occured 4 times.

how can i find it in php?..

使用substr_count

substr_count($str1, $find_str)

Make use of the substr_count php function.

Solution:

$str1 = "when he arrives, he want to open the door.";
$find_str = "he";

substr_count($str1, $find_str);
echo substr_count($str1, $find_str);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM