Home  >  Article  >  Backend Development  >  How to replace text in php

How to replace text in php

coldplay.xixi
coldplay.xixiOriginal
2020-07-13 10:57:324024browse

php替换文字的方法:使用【str_replace()】函数用一些字符串替换字符串中的另一些字符即可,语法为【str_replace(find,replace,string,count)】。

How to replace text 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);
    }

相关学习推荐:PHP编程从入门到精通

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