教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php字符串与字符替换函数

php字符串与字符替换函数

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供php字符串与字符替换函数等资源,欢迎您收藏本站,我们将为您提供最新的php字符串与字符替换函数资源

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6035')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6035>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 可选。一个变量,对替换数进行计数。

 

下面用一款

//-- 程序名称:strreplace()
    //-- 程序用途:替换变量中的非法字符
    //-- 传入参数:变量值
    //********************************************************

   

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9011')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9011>

 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),"<br>",$str);
      $str = str_replace('<',"&lt;",$str);

      $str = str_replace('>',"&gt;",$str);
      $str = str_replace(';',";",$str);
      $str = str_replace('"',"“",$str);
      $str = str_replace("'","‘",$str);
      $str = str_replace(" "," ",$str);
      $str = str_replace("/**/"," ",$str);

      return trim($str);
    }

您可能感兴趣的文章:
php substr_replace函数怎么用
正则表达式处理函数 preg_match,preg_match_all
php5 字符串处理函数汇总
php查找字符串中http地址
php字符串函数有哪些
php表单提交特殊字符过滤方法
PHP字符串函数与使用分析
PHP 替换字符串中的一些字符方法介绍
php字符串函数的简单示例代码
php字符串过滤与转换函数有哪些

[关闭]
~ ~