私は、このテキストの中のすべてのリンクを取得して保存するための、高速で高速なメソッドを探しています。配列。段落からhttpリンクを抽出してPHP上の配列に格納する方法
テキストは平文ASCIIであり、リンクはhttp://thesite.com
またはhttp://www.thesite.com
のような一般的なものです。助けてくれてありがとう。任意の「URL正規表現」のための
私は、このテキストの中のすべてのリンクを取得して保存するための、高速で高速なメソッドを探しています。配列。段落からhttpリンクを抽出してPHP上の配列に格納する方法
テキストは平文ASCIIであり、リンクはhttp://thesite.com
またはhttp://www.thesite.com
のような一般的なものです。助けてくれてありがとう。任意の「URL正規表現」のための
$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);
}
検索グーグルは、その後、次のコードに挿入:$での配列は[0]と一致するよう
preg_match_all("/your url regex here/",$text,$matches);
すべての一致は現在保存されています。
正規表現を使用する必要があります。 pregとeregは両方ともPHPで面白いですが、eregは使いやすく、遅いと考えています。
ここでは、$ textからURLを取得する簡単なpreg呼び出しを示します。
preg_match_all("/https?:\/\/[^\s]+/i", $text, $urls);
$ urlsはURLの配列です。
eregはPHP 5.3で廃止されました。 –
ここではこれらの正規表現はすべていいですし、時間が経つにつれて成長し、最終的には少し違って見えるかもしれません。それは私の信用でもありませんし、理想的です。これは数年後のコミュニティプロジェクトのコードであり、理想だとは言いたくはありませんが、いくつかのニーズに合っています。それをひとつの関数にまとめました:
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\#%~/[email protected]\[\]-]{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\#$%&~/.\-;:=,[email protected]\[\]+]+)#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;
}
正規表現を試しましたか? –
http://php.net/manual/en/function.preg-match.php –
substr()とたくさんの文字列関数もあります... – elvenbyte