Reputation: 5407
I am extracting url from tweets:
sample twitts are:
\'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
I want to get tinyurl from all such user tweets
my code:
<?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 prints actual tweets only
This gives only one url , how can I get all url from tweets?
Upvotes: 1
Views: 270
Reputation: 104
To print just the URL:
echo "<a href='{$url[0]}'>{$url[0]}</a>";
If you want to match all URLs, use 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;
}
Upvotes: 1
Reputation: 1733
Use this as you regex 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`!()\[\]{};:'".,<>?«»“”‘’]))
Check out the source for more info.
Upvotes: 1