Home > Article > Backend Development > How to implement keyword replacement in php
php method to implement keyword replacement: first use the strpos() function to find the location of the keyword, such as [strpos($content,$search)]; then use the substr_replace() function to replace the keyword .
substr_replace() function replaces part of a string with another string and returns the replaced string. If string is an array, the array is returned.
(Recommended tutorial: php graphic tutorial)
Syntax:
substr_replace(string,replacement,start,length)
If the start parameter is a negative number and length is less than or equal to start, then length is 0.
(Video tutorial recommendation: php video tutorial)
Code implementation:
//首先找到关键字所在位置,然后使用 substr_replace(系统函数)进行替换操作 function str_replace_once($search,$replace,$content){ //把图片描述去掉 $content=preg_replace("/alt=([^ >]+)/is",'',$content); $pos=strpos($content,$search); if($pos===false){ return $haystack; } return substr_replace($content,$replace,$pos,strlen($search)); }
The above is the detailed content of How to implement keyword replacement in php. For more information, please follow other related articles on the PHP Chinese website!