preg_replace字符替换例子
这里介绍三种常用方法.
代码如下
方法一:
$1",$txt);
?>
方法二:
$1",$txt);
?>
方法三:
$1",$txt);
?>
三种方法都返回同样结果.. PHP中的Perl风格正则与Perl完全一样.连quotemeta也是通用的..
str_replace() 函数使用一个字符串替换字符串中的另一些字符
function strreplace($str){
$str = stripslashes($str);
$str = str_replace(chr(92),'',$str);
$str = str_replace(chr(47),'',$str);
$str = str_replace(chr(10).chr(13),"
",$str);
$str = str_replace('
$str = str_replace('>',">",$str);
$str = str_replace(';',";",$str);
$str = str_replace('"',"“",$str);
$str = str_replace("'","‘",$str);
$str = str_replace(" "," ",$str);
$str = str_replace("/**/"," ",$str);
return trim($str);
}
双引号替换问题
$tmp_data= str_replace("\"","",$tmp_data);双引号替换为空字符串
$tmp_data= str_replace("\"","'",$tmp_data);双引号替换为单引号
当然ereg_replace 作为正则表达式必须关注的,也要提示一下