我正在从推文中提取网址:
示例twitts是:
\'Whn I ws calld for ths song @VishalDadlani @5hekhar both said \"we finally hv the perfect song for u\" lulz http://t.co/6vzbyjfCGk #DramaQueen\'
\'#FreeSunder @PMOIndia The world watches as India allows the torture of temple elephants to continue. PLEASE SHARE http://t.co/g8f1kgdasU\'
india
我希望从所有此类用户推文中获得微小的推文
我的代码:
<?php
$reg_exUrl = "/(?<![\>https?:\/\/|href=\"'])(?<http>(https?:[\/][\/]|www\.)([a-z]|[A-Z]|[0-9]|[\/.]|[~])*)/";
// The Text you want to filter for urls
$text = "Whn I ws calld for ths song lulz http://t.co/6vzbyjfCGk #DramaQueen\ ";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url))
{
echo preg_replace($reg_exUrl, "<a href='{$url[0]}'>{$url[0]}</a> ", $text);
} else { // if no urls in the text just return the text
echo $text;
}
echo here仅显示实际的推文
这只提供一个网址,如何从推文中获取所有网址?
答案 0 :(得分:1)
使用此正则表达式URL:
(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
查看source了解详情。
答案 1 :(得分:1)
仅打印网址:
echo "<a href='{$url[0]}'>{$url[0]}</a>";
如果您想匹配所有网址,请使用preg_match_all
if(preg_match($reg_exUrl, $text, $url)) {
preg_match_all($reg_exUrl, $text, $urls);
foreach ($urls[0] as $url) {
echo "<a href='{$url}'>{$url}</a><br>";
}
} else {
echo $text;
}