有谁知道我可以用来查找字符串中的URL的正则表达式?我在Google上发现了很多正则表达式,用于确定整个字符串是否为URL,但我需要能够在整个字符串中搜索URL.例如,我希望能够找到www.google.com并http://yahoo.com在以下字符串中:
Hello www.google.com World http://yahoo.com
Run Code Online (Sandbox Code Playgroud)
我不是在寻找字符串中的特定URL.我正在寻找字符串中的所有URL,这就是我需要正则表达式的原因.
我们如何使用PHP来识别字符串中的URL并将它们存储在数组中?
explode如果URL包含逗号,则无法使用该函数,它不会给出正确的结果.
使用PHP,我如何模仿Stack Overflow的自动链接行为(BTW非常酷)?
例如,以下URL:
http://www.stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior
转换成这个:
<a title="how to mimic stackoverflow auto link behavior" rel="nofollow" href="http://www.stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior">stackoverflow.com/questions/1925455/…</a>
Run Code Online (Sandbox Code Playgroud)
title在这种情况下,我并不真正关心属性.
还有这个:
转换成这个:
<a rel="nofollow" href="http://pt.php.net/manual/en/function.base-convert.php#52450">pt.php.net/manual/en/…</a>
Run Code Online (Sandbox Code Playgroud)
如何在PHP中创建类似的功能?
PS:查看我对这个问题的评论,了解更多示例和行为.
我想找到文本中的所有链接,如下所示:
Test text http://hello.world Test text
http://google.com/file.jpg Test text https://hell.o.wor.ld/test?qwe=qwe Test text
test text http://test.test/test
Run Code Online (Sandbox Code Playgroud)
我知道我需要使用preg_match_all,但只有头脑中的想法:从http | https | ftp开始搜索并在文本的空格或末尾出现的结束搜索,这就是我真正需要的所有内容,因此所有链接都可以正确找到.
任何人都可以帮助我使用php regexp模式?
我想我需要在模式结束时使用断言,但现在无法理解它们的正确用法.
有任何想法吗?感谢名单!
我有此功能来识别和转换标签,表情符号等
function convert_text($str) {
$regex = "/[@#](\w+)/";
//type and links
$hrefs = [
'#' => 'hashtag.php?hashtag',
'@' => 'user.php?user'
];
$result = preg_replace_callback($regex, function($matches) use ($hrefs) {
return sprintf(
'<a href="%s=%s">%s</a>',
$hrefs[$matches[0][0]],
$matches[1],
$matches[0]
);
}, $str);
//$result = preg_replace("/U\+([A-F0-9]{5})/", '\u{${1}}', $result);
$result = preg_replace('/U\+([A-F0-9]{5})/', '<span style="font-size:30px;">&#x\\1;</span>', $result);
return ($result);
}
Run Code Online (Sandbox Code Playgroud)
我想,使其认识http://并https://从文字和比转换为:
<a href="http://link.com">http://link.com</a>
如何在函数内部实现呢?