与正则表达式链接的 PHP URL
我知道我已经在很多地方看到过这种做法,但我需要一些与常规不同的东西。可悲的是,当我在任何地方搜索它时,它都会被埋在关于将链接变成 html 标签链接的帖子中。我希望 PHP 函数从链接中删除“http://”和“https://”以及 .* 之后的任何内容,所以基本上我要寻找的是将 A 转换为 B。
A: http://www.youtube.com/watch?v=spsnQWtsUFM
B: <a href="http://www.youtube.com/watch?v=spsnQWtsUFM">www.youtube.com</a>
如果有帮助,这是我当前的 PHP 正则表达式替换函数。
ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\" class=\"bwl\" target=\"_new\">\\0</a>", htmlspecialchars($body, ENT_QUOTES)));
说我完全不理解正则表达式可能也会有帮助。谢谢!
编辑:当我输入这样的评论 blahblah https://www.facebook .com/?sk=ff&ap=1 blah
我得到这样的 htmlhttps://www.facebook.com/?sk=ff&ap=1 blah">www.facebook.com
根本不起作用,因为它正在使用链接周围的文本。如果有人只评论一个链接,效果会很好。这是我将功能更改为这个的时候
preg_replace("#^(.*)//(.*)/(.*)$#",'<a class="bwl" href="\0">\2</a>', htmlspecialchars($body, ENT_QUOTES));
I know I've seen this done a lot in places, but I need something a little more different than the norm. Sadly When I search this anywhere it gets buried in posts about just making the link into an html tag link. I want the PHP function to strip out the "http://" and "https://" from the link as well as anything after the .* so basically what I am looking for is to turn A into B.
A: http://www.youtube.com/watch?v=spsnQWtsUFM
B: <a href="http://www.youtube.com/watch?v=spsnQWtsUFM">www.youtube.com</a>
If it helps, here is my current PHP regex replace function.
ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\" class=\"bwl\" target=\"_new\">\\0</a>", htmlspecialchars($body, ENT_QUOTES)));
It would probably also be helpful to say that I have absolutely no understanding in regular expressions. Thanks!
EDIT: When I entered a comment like this blahblah https://www.facebook.com/?sk=ff&ap=1 blah
I get html like this<a class="bwl" href="blahblah https://www.facebook.com/?sk=ff&ap=1 blah">www.facebook.com</a>
which doesn't work at all as it is taking the text around the link with it. It works great if someone only comments a link however. This is when I changed the function to this
preg_replace("#^(.*)//(.*)/(.*)$#",'<a class="bwl" href="\0">\2</a>', htmlspecialchars($body, ENT_QUOTES));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是最简单、最干净的方法:
编辑:我假设 $str 首先被检查为 URL,所以我将其排除在外。另外,我假设所有 URL 都将包含“http://”或“https://”。如果网址格式如下
www.youtube.com/watch?v=spsnQWtsUFM
甚至youtube.com/watch?v=spsnQWtsUFM
,则上述正则表达式将无效不工作!EDIT2:很抱歉,我没有意识到您正在尝试替换整个测试中的所有字符串。在这种情况下,这应该按照您想要的方式工作:
This is the simples and cleanest way:
EDIT: I assume that the $str had been checked to be a URL in the first place, so I left that out. Also, I assume that all the URLs will contain either 'http://' or 'https://'. In case the url is formatted like this
www.youtube.com/watch?v=spsnQWtsUFM
or evenyoutube.com/watch?v=spsnQWtsUFM
, the above regexp won't work!EDIT2: I'm sorry, I didn't realize that you were trying to replace all strings in a whole test. In that case, this should work the way you want it:
我也不是正则表达式高手,
当我尝试在程序员的记事本中使用查找和替换时,这对我有用。
^(.)// 应提取协议 - 在第二行中称为 \1。
(.)/ 应该提取所有内容,直到第一个 / - 在第二行中称为 \2。
(.*)$ 捕获字符串末尾之前的所有内容。 - 在第二行中称为 \3。
稍后添加
这应该会好一点,但只会替换 1 个 URL
I am not a regex whizz either,
was what worked for me when I tried to use as find and replace in programmer's notepad.
^(.)// should extract the protocol - referred as \1 in the second line.
(.)/ should extract everything till the first / - referred as \2 in the second line.
(.*)$ captures everything till the end of the string. - referred as \3 in the second line.
Added later
This should be a bit better, but will only replace just 1 URL
\0 被整个匹配的字符串替换,而 \x(其中 x 是从 1 开始的 0 以外的数字)将根据括号中的内容以及这些组出现的顺序被匹配字符串的每个子部分替换。您的解决方案如下:
但我无法对此进行测试,所以请告诉我它是否有效。
The \0 is replaced by the entire matched string, whereas \x (where x is a number other than 0 starting at 1) will be replaced by each subpart of your matched string based on what you wrap in parentheses and the order those groups appear. Your solution is as follows:
I haven't been able to test this though so let me know if it works.
我认为这应该可以做到(我还没有测试过):
I think this should do it (I haven't tested it):
我很惊讶没有人记得 PHP 的 parse_url 函数:
我想你知道从那里该做什么。
I'm surprised no one remembers PHP's parse_url function:
I think you know what to do from there.
$result = preg_replace('%(http[s]?://)(\S+)%', '\2', $主题);
$result = preg_replace('%(http[s]?://)(\S+)%', '<a href="\1\2">\2</a>', $subject);
使用正则表达式的代码不能完全工作。
我做了这个代码。它更全面,但有效:
请在此处查看结果:http://cht。 dk/data/php-scripts/inc_functions_links.php
在此处查看源代码:http://cht.dk/data/php-scripts/inc_functions_links.txt
The code with regex does not work completely.
I made this code. It is much more comprehensive, but it works:
See the result here: http://cht.dk/data/php-scripts/inc_functions_links.php
See the source code here: http://cht.dk/data/php-scripts/inc_functions_links.txt