本文介绍了正则表达式没有获得特殊字符的完整链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此代码:
$string = preg_replace("~(?!(?:https?://(?:www\.)?|www\.)(?:youtube\.com)(?:https?://(?:www\.)?|www\.)[\w./=?#-]+~i", '<a href="$0">$0</a>', $string);
这样我就可以将下面的链接变成可点击的链接。
So that I can turn the link beneath into a clickable link.
http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton%2C_Yitzhak_Rabin,%2C_Yasser_Arafat_at_the_White_House_1993-09-13.jpg
这适用于某个部分......它建立了的链接http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton
但其余部分保留纯文本。如何使其适用于整个链接?所以你也看到逗号
...我怎么能把它作为一个链接吗?
This works for a part... It makes a link of http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton
but the rest stays plain text. How can I make it work for the whole link? So you see a comma
too... How can I make that a link?
推荐答案
我也试图在URL的末尾说明标点符号(这样我们就不要不包括它。)
Also I'm trying to account for punctuation at the end of a URL (so that we don't include it).
<?php
$string = "This works http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton%2C_Yitzhak_Rabin%2C_Yasser_Arafat_at_the_White_House_1993-09-13.jpg. This one should fail http://www.youtube.com/v/adlskdfjasopie. Although this one should fail as well http://youtu.be/adlkajdaslk.";
$string = preg_replace("~(?!(?:https?://(?:www\.)?|www\.)(?:youtu))(?:https?://(?:www\.)?|www\.)[^\s]+[^.!?,\<\]\[\)(]~i",'<a href="$0">$0</a>',$string);
?>
输出
This works <a href="http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton%2C_Yitzhak_Rabin%2C_Yasser_Arafat_at_the_White_House_1993-09-13.jpg. ">http://upload.wikimedia.org/wikipedia/commons/f/f2/Bill_Clinton%2C_Yitzhak_Rabin%2C_Yasser_Arafat_at_the_White_House_1993-09-13.jpg. </a>This one should fail http://www.youtube.com/v/adlskdfjasopie. Although this one should fail as well http://youtu.be/adlkajdaslk.
这篇关于正则表达式没有获得特殊字符的完整链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!