Home > Backend Development > PHP Tutorial > 正则表达式:以模式后为/或字符串结束

正则表达式:以模式后为/或字符串结束

WBOY
Release: 2016-06-13 11:57:38
Original
952 people have browsed it

求助正则表达式:以模式后为/或字符串结束
         我想一个匹配模式后面跟着/或字符串结束。

需求如下:两段域名修改为三段(增加www.)

原字符串:http://a.com 或 http://a.com/ 或 http://www.a.com/
替换为:http://www.a.com

http://k.a.com 不能替换为 http://www.k.a.com

我写的:
preg_replace('/(\/\/)([^.\/]+\.[^.\/]+$)/i', '$1www.$2', 'http://weibo.com');
preg_replace('/(\/\/)([^.\/]+\.[^.\/]+\/)/i', '$1www.$2', 'http://weibo.com/');
需要两次替换,能不能把\/和$放在一起,类似于[a2]
------解决方案--------------------

preg_replace('/(\/\/)([^.\/]+\.[^.\/]+(?:\/<br><font color='#FF8000'>------解决方案--------------------</font><br>$))/i', '$1www.$2', 'http://weibo.com');
Copy after login

------解决方案--------------------
试试
echo preg_replace('/(\/\/)([^.\/]+\.[^.\/]+)($<br><font color='#FF8000'>------解决方案--------------------</font><br>\/)(.+)?/ie', '"$1"."www."."$2".("$4" ? "/"."$4" : "")', 'http://weibo.com/aa');
Copy after login

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