Home  >  Article  >  Backend Development  >  How to implement keyword replacement in php

How to implement keyword replacement in php

王林
王林Original
2020-08-03 13:38:042391browse

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 .

How to implement keyword replacement in php

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!

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