【发布时间】:2011-10-15 05:39:20
【问题描述】:
我在 php 上的 var 中有一个大文本,我正在寻找一种好的快速方法来检索该文本中的所有链接并将它们存储到一个数组中。
文本是纯 ascii,链接是常见的链接,如 http://thesite.com 或 http://www.thesite.com。感谢您的帮助。
【问题讨论】:
-
你还有substr(),还有很多字符串函数……
标签: php
我在 php 上的 var 中有一个大文本,我正在寻找一种好的快速方法来检索该文本中的所有链接并将它们存储到一个数组中。
文本是纯 ascii,链接是常见的链接,如 http://thesite.com 或 http://www.thesite.com。感谢您的帮助。
【问题讨论】:
标签: php
$text = 'Lorem ipsum http://thesite.com dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt https://www.thesite.com ut labore et dolore magna aliqua. Ut http://www.thesite.com enim ad minim veniam,';
$pattern = '!(https?://[^\s]+)!'; // refine this for better/more specific results
if (preg_match_all($pattern, $text, $matches)) {
list(, $links) = ($matches);
print_r($links);
}
【讨论】:
在 google 中搜索任何“URL Regex”,然后将其插入以下代码:
preg_match_all("/your url regex here/",$text,$matches);
所有匹配项现在都作为数组存储在 $matches[0] 中。
【讨论】:
这里的这些正则表达式都很不错,但是,它们会随着时间的推移而增长,最终,事情可能看起来有点不同。这不是我的全部功劳,也不是很理想,这是一个社区项目的代码,已经有几年的历史了,我不想说它是理想的,但它适合一些需求。把它编译成一个函数:
echo make_clickable('test http://www.google.com/');
/**
* make_clickable
*
* make a text clickable
*
* @param string $text to make clickable
* @param callback $url callback to process URLs
* @return string clickable text
* @author hakre and contributors
* @license GPL
*/
function make_clickable($text, $url = null) {
if (null === $url)
$callback_url = function($url) {return $url;};
else
$callback_url = $url;
$ret = ' ' . $text;
// urls
$save = ini_set('pcre.recursion_limit', 10000);
$retval = preg_replace_callback('#(?<!=[\'"])(?<=[*\')+.,;:!&$\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#%~/?@\[\]-]{1,2000}|[\'*(+.,;:!=&$](?![\b\)]|(\))?([\s]|$))|(?(1)\)(?![\s<.,;:]|$)|\)))+)#is', function($matches) use ($callback_url)
{
$url = $matches[2];
$suffix = '';
/** Include parentheses in the URL only if paired **/
while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
$suffix = strrchr( $url, ')' ) . $suffix;
$url = substr( $url, 0, strrpos( $url, ')' ) );
}
$url = $callback_url($url);
if ( empty($url) )
return $matches[0];
return $matches[1] . "<a href=\"$url\">$url</a>" . $suffix;
}, $ret);
if (null !== $retval )
$ret = $retval;
ini_set('pcre.recursion_limit', $save);
// web ftp
$ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', function ($matches) use ($callback_url)
{
$ret = '';
$dest = $matches[2];
$dest = 'http://' . $dest;
$dest = $callback_url($dest);
if ( empty($dest) )
return $matches[0];
// removed trailing [.,;:)] from URL
if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
$ret = substr($dest, -1);
$dest = substr($dest, 0, strlen($dest)-1);
}
return $matches[1] . "<a href=\"$dest\">$dest</a>$ret";
}, $ret);
// email
$ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', function($matches)
{
$email = $matches[2] . '@' . $matches[3];
return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
}, $ret);
$ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
$ret = trim($ret);
return $ret;
}
【讨论】:
你必须使用正则表达式。 preg 和 ereg 在 PHP 中都很有趣,因为 ereg 更易于使用,但速度较慢。
这是一个简单的 preg 调用,它将从 $text 获取 URL。
preg_match_all("/https?:\/\/[^\s]+/i", $text, $urls);
$urls 是您的 URL 数组。
【讨论】: