Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何使用积极的前瞻来替换字符串中除了一个 http(s):// 之外的所有内容?
我的用户输入有时在字符串中包含多个 http:// 或 https://,例如http://http://wwww.site.com/,我需要删除所有实例,但只有一个。我已经读过关于在正则表达式模式中使用积极的前瞻,但似乎无法使其工作。
http://http://wwww.site.com/
我尝试了以下方法:
preg_replace( 'https?://(?=.*https?://)', '', $url );
$url = preg_replace("#(https?://)+#", "$1", $url);
这可以工作:
<?php $text = 'http://https://http://http://https://abc.com'; $text = preg_replace('#(https?://)+https?://#iU', '', $text); echo $text;