Home >Backend Development >PHP Tutorial >PHP multiple keywords replace some keywords

PHP multiple keywords replace some keywords

WBOY
WBOYOriginal
2016-08-25 10:19:391580browse

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

Reply content:

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn