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

How to implement text replacement in php

藏色散人
藏色散人Original
2020-11-10 10:13:433745browse

php实现文字替换的方法:首先创建一个PHP示例文件;然后输入代码“str_replace("iwind", "kiki", "i love iwind, iwind said");”;最后输出执行结果即可。

How to implement text replacement in php

推荐:《PHP视频教程

在php替换字符效率最高也是最简单字符替换函数str_replace($arr1,$arr2,$str)

实例一

str_replace("iwind", "kiki", "i love iwind, iwind said");

将输出 "i love kiki, kiki said"

结果

即将 原字符串中的所有"iwind"都替换成了"kiki".str_replace是大小写敏感的,所以对你不能设想用 str_replace("iwind", "kiki",...)替换原字符串中的"iwind". str_replace还可以实现多对一

用法

str_replace() 函数使用一个字符串替换字符串中的另一些字符。

语法

str_replace(find,replace,string,count)参数 

find 必需。规定要查找的值。 

replace 必需。规定替换 find 中的值的值。 

string 必需。规定被搜索的字符串。 

count 可选。一个变量,对替换数进行计数。

function strreplace($str){
      $str = strips教程lashes($str);
      $str = str_replace(chr(92),'',$str);
      $str = str_replace(chr(47),'',$str);
      $str = str_replace(chr(10).chr(13),"",$str);
      $str = str_replace(&#39;<&#39;,"&lt;",$str);      $str = str_replace(&#39;>&#39;,"&gt;",$str);
      $str = str_replace(&#39;;&#39;,";",$str);
      $str = str_replace(&#39;"&#39;,"“",$str);
      $str = str_replace("&#39;","‘",$str);
      $str = str_replace(" "," ",$str);
      $str = str_replace("/**/"," ",$str);
      return trim($str);
    }

The above is the detailed content of How to implement text 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