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

正则匹配解决方案

WBOY
Release: 2016-06-13 11:06:54
Original
838 people have browsed it

正则匹配
$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次 

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template