Home >Backend Development >PHP Tutorial >PHP multiple keywords replace some keywords
php
For example, an article $content has 10 keywords A. I want to replace the first three A with B. How to do it? It is best to use str_replace
You can use the preg_replace() function,
demo:
$content = 'a123b123c123d123e';
//Parameter 3 means only replacing 3 times
$str = preg_replace('/123/','999',$content,3,$count);
echo 'Number of substitutions:'.$count,'
';
echo 'Replacement result:' ,$str;
? >
Run result:
Number of replacements: 3
Replacement result: a999b999c999d123e
View the article tutorial](http://www.w3school.com.cn/php/func_string_str_replace.asp "") There is a matching number count in it. Just write the count parameter list