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:
.但是,如果我在正则表达式中尝试了其中任何一个,它就可以工作了。如果我在表达式中同时使用两者,它不会替换http:
请参阅我正在使用的嵌入式脚本,
1. <iframe frameborder="0" scrolling="no" id="chat_embed" src="http://twitch.tv/chat/embed?channel=athenelive&popout_chat=true" height="500" width="350"></iframe>
->有 http:(需要删除)
2. <iframe width="560" height="315" src="//www.youtube.com/embed/JxHOjZLRxAg" frameborder="0" allowfullscreen></iframe>
->如果我在正则表达式中使用 http:单独使用此 youtube 嵌入式脚本不被视为 youtube URL。出于两个兼容的目的,我使用了http|http:
.
更新:
我需要从上面给出的第一帧中删除 http:。
应该看线
<iframe frameborder="0" scrolling="no" id="chat_embed" src="//twitch.tv/chat/embed?channel=athenelive&popout_chat=true" height="500" width="350"></iframe>
http:
不起作用,因为您在捕获组后放置了一个冒号:
(http|http:|ftp|scp)(s)?:
^ ^
要解决此问题,请从捕获组中删除http:
(这是无用的):
(http|ftp|scp)(s)?:
我认为您正在寻找:
$url = preg_replace('~'bhttps?:~', '', $url);
将(http|http:)
替换为
http:?
在交替中,正则表达式引擎会按顺序尝试匹配。在http://sthg.com
中,http
匹配,所以没有理由尝试http:
变体。(使用(http:|http)
实际上对您有用)
使用贪婪的:?
将解决问题。
此外,为了改进您的正则表达式,(s)?
周围的括号是无用的(s?
就足够了),如果您不将其用作正则表达式分隔符,/
不是特殊字符(这里您使用的是!
),因此无需转义它们。
如果要剥离 http://只需执行以下操作:
$url = str_replace('http:', '', $my_url);
据我了解这个问题 - 您可能最好在初始"//"之前删除所有内容 - 看起来您希望 URL 显示为//www.somewhere.com/something/something/darkside/something/ - 因此使用这样的正则表达式可能更适合您的目的:
$url = preg_replace('|^[^/]*(//.*)$|', "$1", $feed->feed_text);