本文实例讲述了php中preg_replace正则替换用法。分享给大家供大家参考,具体如下:
1.php 的 preg_replace 与 str_replace 都是默认 /g 的,全部替换
2.如果需要使用正则表达式 需要使用preg_replace
<?php
$a = "abc defa
bcd ef";
$b= preg_replace("/\t|a/","",$a);
echo($b);
/*
输出:
bc def
bcd ef
*/
?>
另外对比一下js中的replace,感觉php的语法 不优美
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
var a="a b";
console.log(a.replace(/\t/g,""));
</script>
</body>
</html>
运行效果图如下:
[img]http://files.jb51.net/file_images/article/201701/2017117151320848.png?2017017151415[/img]
[b]PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:[/b]
[b]JavaScript正则表达式在线测试工具:
[/b][url=http://tools.jb51.net/regex/javascript]http://tools.jb51.net/regex/javascript[/url]
[b]正则表达式在线生成工具:
[/b][url=http://tools.jb51.net/regex/create_reg]http://tools.jb51.net/regex/create_reg[/url]
更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/180.htm]php正则表达式用法总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/357.htm]PHP运算与运算符用法总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/495.htm]PHP网络编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》
希望本文所述对大家PHP程序设计有所帮助。