Tried this,
$url = preg_replace('!(http|http:|ftp|scp)(s)?:\/\/[a-zA-Z0-9.=?&-_/]+!', "<a style=text-decoration:underline;color:blue href=\"\\0\" target='_blank' >\\0</a>",$feed->feed_text)):
我想从网址中替换http和http:我试过上面的那个替换http
而不是http:
。但是如果我在正则表达式中尝试任何一个它正在工作。如果我在表达式中使用它们,它不会替换{{ 1}}
请参阅我正在使用的嵌入式脚本,
1. http:
- &gt;拥有http :(需要删除)
2. <iframe frameborder="0" scrolling="no" id="chat_embed" src="http://twitch.tv/chat/embed?channel=athenelive&popout_chat=true" height="500" width="350"></iframe>
- &gt;如果我在regex中单独使用http:这个youtube嵌入式脚本不会被视为youtube网址。出于兼容的目的,我使用了<iframe width="560" height="315" src="//www.youtube.com/embed/JxHOjZLRxAg" frameborder="0" allowfullscreen></iframe>
。
更新
我需要从上面给出的第一帧中删除http:。
应该看行
http|http:
答案 0 :(得分:2)
它对http:
不起作用,因为你在捕获组后放了一个冒号:
(http|http:|ftp|scp)(s)?:
^ ^
要解决此问题,请从捕获组中删除http:
(这是无用的):
(http|ftp|scp)(s)?:
我认为你正在寻找:
$url = preg_replace('~\bhttps?:~', '', $url);
答案 1 :(得分:0)
将(http|http:)
替换为
http:?
在交替时,正则表达式引擎按顺序尝试匹配。在http://sthg.com
,http
匹配,因此没有理由尝试http:
变体。 (使用(http:|http)
实际上适合你)
使用贪婪的:?
将解决问题。
另外,为了改善你的正则表达式,(s)?
周围的括号是无用的(s?
就足够了)如果不将它用作正则表达式,/
不是特殊字符分隔符(这里你使用!
)所以不需要逃避它们。
答案 2 :(得分:0)
如果要删除http://,请执行以下操作:
$url = str_replace('http:', '', $my_url);
答案 3 :(得分:0)
我理解这个问题 - 你可能最好只删除最初的'//'之前的所有内容 - 看起来好像你希望网址显示为 // www.somewhere.com/something/something / darkside / something / - 所以使用这样的RegExp可能更适合您的目的:
$url = preg_replace('|^[^/]*(//.*)$|', "$1", $feed->feed_text);