我有这个文字:
$string = "this is my friend's website http://example.com I think it is coll";
Run Code Online (Sandbox Code Playgroud)
如何将链接提取到另一个变量?
我知道它应该是通过使用正则表达式,preg_match()但我不知道如何?
Nob*_*obu 45
可能最安全的方法是使用WordPress的代码片段.下载最新版本(目前为3.1.1)并查看wp-includes/formatting.php.有一个名为make_clickable的函数,它有param的纯文本并返回格式化的字符串.您可以获取用于提取URL的代码.虽然这很复杂.
这一行正则表达式可能会有所帮助.
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match);
Run Code Online (Sandbox Code Playgroud)
但是这个正则表达式仍然无法删除一些格式错误的URL(例如http://google:ha.ckers.org).
另请参见: 如何模拟StackOverflow自动链接行为
Mik*_*oos 15
我尝试按照Nobu的说法,使用Wordpress,但是为了与其他WordPress函数有很多依赖关系,我选择使用Nobu的正则表达式preg_match_all()并将其转换为函数,使用preg_replace_callback(); 一个函数,它现在用可点击的链接替换文本中的所有链接.它使用匿名函数,因此您需要PHP 5.3,或者您可以重写代码以使用普通函数.
<?php
/**
* Make clickable links from URLs in text.
*/
function make_clickable($text) {
$regex = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';
return preg_replace_callback($regex, function ($matches) {
return "<a href=\'{$matches[0]}\'>{$matches[0]}</a>";
}, $text);
}
Run Code Online (Sandbox Code Playgroud)
preg_match_all('/[a-z]+:\/\/\S+/', $string, $matches);
Run Code Online (Sandbox Code Playgroud)
这是一种简单的方法,适用于很多情况,而不是所有情况。所有匹配项都放在 $matches 中。请注意,这不包括锚元素(<a href=""...)中的链接,但这也不在您的示例中。
你可以这样做..
<?php
$string = "this is my friend's website http://example.com I think it is coll";
echo explode(' ',strstr($string,'http://'))[0]; //"prints" http://example.com
Run Code Online (Sandbox Code Playgroud)
适合我的代码(特别是如果你的$ string中有几个链接)是:
$string = "this is my friend's website http://example.com I think it is cool, but this is cooler http://www.memelpower.com :)";
$regex = '/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
preg_match_all($regex, $string, $matches);
$urls = $matches[0];
// go over all links
foreach($urls as $url)
{
echo $url.'<br />';
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助他人.