Hi,

I am trying to build a Twiiter website widget using the Twitter API, that will display a list of recent tweets.

I am stuck on one piece which is the display of the URLs in the tweet.

I am using preg_replace to look through the text and find a match and replace etc.. as below:-

$text = preg_replace('/(https{0,1}:\/\/[\w\-\.\/#?&=]*)/', '<span class="tweet_links_intext"><a href="$1" target="_blank">$1</a></span>', $text);

This works perfectly correctly but with one exception, the links are now displayed with the preceeding http://

How can I get the links to be displayed and be clickable without the preceeding http://

I have tried changing the above code around by replacing the $1 between the anchor tags with the actual entity tag from the Twitter API i.e.

status->user->display_url

but this does not work at all.

Any help would be appreciated.

Kind regards..,

MT

How about this

$text = preg_replace('/(https{0,1}:\/\/([\w\-\.\/#?&=]*))/', '<span class="tweet_links_intext"><a href="$1" target="_blank">$2</a></span>', $text);

How about this

$text = preg_replace('/(https{0,1}:\/\/([\w\-\.\/#?&=]*))/', '<span class="tweet_links_intext"><a href="$1" target="_blank">$2</a></span>', $text);

Thanks for that, it certainly done the trick.

For my future reference, why & what is the difference between the $1 & $2?

How does using $2 remove the htt:// part from the URL?

Kind regards..,

MT

I've placed an additional subpattern in the matching pattern which matches only the part of the link after the http://
It basically matches like this: ([url]http://(www.website.com[/url])) which sets the following back-references for the replacement

$1 = [url]http://www.website.com[/url]
$2 = [url]www.website.com[/url]
commented: spot on, thx +3

I've placed an additional subpattern in the matching pattern which matches only the part of the link after the http://
It basically matches like this: ([url]http://(www.website.com[/url])) which sets the following back-references for the replacement

$1 = [url]http://www.website.com[/url]
$2 = [url]www.website.com[/url]

Thanks for the info

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.