php 使用php从字符串中查找url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9151681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:15:47  来源:igfitidea点击:

Find url from string with php

php

提问by user1161867

following code is used to find url from a string with php. Here is the code:

以下代码用于从带有 php 的字符串中查找 url。这是代码:

$string = "Hello http://www.bytes.com world www.yahoo.com";
preg_match('/(http:\/\/[^\s]+)/', $string, $text);
$hypertext = "<a href=\"". $text[0] . "\">" . $text[0] . "</a>";
$newString = preg_replace('/(http:\/\/[^\s]+)/', $hypertext, $string);
echo $newString;

Well, it shows a link but if i provide few link it doesn't work and also if i write without http:// then it doesn't show link. I want whatever link is provided it should be active, Like stackoverflow.com.

好吧,它显示了一个链接,但如果我提供的链接很少,它就不起作用,而且如果我在没有 http:// 的情况下写入,则它不会显示链接。我想要任何提供的链接都应该是活动的,比如 stackoverflow.com。

Any help please..

任何帮助请..

回答by axiomer

A working method for linking with http/https/ftp/ftps/scp/scps: $newStr = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?&_/]+!', "<a href=\"\\0\">\\0</a>",$str);

一种与http/https/ftp/ftps/scp/scps链接的工作方法: $newStr = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?&_/]+!', "<a href=\"\\0\">\\0</a>",$str);

I strongly advise NOT linking when it only has a dot, because it will consider PHP 5.2, ASP.NET, etc. links, which is hardly acceptable.

我强烈建议当它只有一个点时不要链接,因为它会考虑 PHP 5.2、ASP.NET 等链接,这是很难接受的。

Update:if you want www. strings as well, take a look at this.

更新:如果你想要 www. 字符串也是如此,看看这个

回答by bummzack

If you want to detect something like stackoverflow.com, then you're going to have to check for all possible TLDs to rule out something like Web 2.0, which is quite a long list. Still, this is also going to match something as ASP.NETetc.

如果您想检测类似 的内容stackoverflow.com,则必须检查所有可能的 TLD 以排除类似 的内容Web 2.0,这是一个很长的列表。尽管如此,这也将匹配诸如此类的东西ASP.NET

The regex would looks something like this:

正则表达式看起来像这样:

$hypertext = preg_replace(
    '{\b(?:http://)?(www\.)?([^\s]+)(\.com|\.org|\.net)\b}mi',
    '<a href="http://"></a>',
    $text
);

This only matches domains ending in .com, .org and .net... as previously stated, you would have to extend this list to match all TLDs

这仅匹配以 .com、.org 和 .net 结尾的域...如前所述,您必须扩展此列表以匹配所有 TLD

回答by Piotr Olaszewski

@axiomer your example wasn't work if link will be in format:

@axiomer 如果链接采用以下格式,您的示例将不起作用:

https://stackoverflow.com?val1=bla&val2blablabla%20bla%20bla.bl

https://stackoverflow.com?val1=bla&val2blablabla%20bla%20bla.bl

correct solution:

正确的解决方法:

preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?%=&_/]+!', "<a href=\"\0\">\0</a>", $content);

produces:

产生:

<a href="https://stackoverflow.com?val1=bla&val2blablabla%20bla%20bla.bl">https://stackoverflow.com?val1=bla&val2blablabla%20bla%20bla.bl</a>