Home  >  Article  >  Backend Development  >  正则匹配解决方案

正则匹配解决方案

WBOY
WBOYOriginal
2016-06-13 12:33:43791browse

正则匹配
$url="http://www.baidu.comhttp://www.163.com";
$u=preg_replace("/\.\w+\./i",".231.",$url);
echo $u;

结果:http://www.231.comhttp://www.231.com

吧\w换成 . 后

结果:http://www.231.com

为什么
------解决方案--------------------
默认是贪婪匹配
$u=preg_replace("/\..+?\./i",".231.",$url); //这样就行了
------解决方案--------------------
? 表示匹配0次或1次 

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