Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg replace urls in RSS feed

Locating and replacing urls as anchors using SimplePie



1:59 pm on Jul 7, 2009 (gmt 0)

5+ Year Member

I'm using SimplePie to parse a Twitter feed but of course this strips out the links in the "Tweets". I have looked at using the JS applet supplied by Twitter but it's far too slow at, well, working.

Anyway the general idea is that I want to be able to locate the urls in the "Tweet" string and then change them from "http://www.example.com" to

<a href="http://www.example.com">http://www.example.com</a>

This has been frustrating me for a couple of days now, I have tried various regular expressions and have trawled the web (and these forums) trying to find a way to do this, all I've managed to find so far are a couple that haven't worked for me. The problem is I'm not that great at regular expressions, they're kind of new to me really, I understand how they work in principal but actually applying them is a different kettle of fish! The link could be anywhere in the text...

Also generally speaking they will be in the form "http://bit.ly/123456" where "123456" is a string of any numbers/letters. I don't want to exclusively pick these types of urls though either.

Hope that's enough information!

Thanks in advance


4:10 pm on Jul 7, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

Try this just pass in your string

function getLink($string){

//keep this as the original
$constant = $string;

//remove any line breaks
$string = ereg_replace("\n", " ", $string);

//add one white space to the end in case
//there is a link as the very last thing
$string .= " ";

//get the first http:
$pos = stripos($string, "http:");

//an array of the links we need to return
$links = array();

//while we have http keep going through it
while($pos !== false){

$end = (stripos($string, " ", $pos));
$url = substr($string, $pos, ($end - $pos));

array_push($links, $url);

$string = substr($string, $end);
$pos = stripos($string, "http:");


//now we replace the string
foreach($links as $i){

$constant = str_ireplace($i, "<a href=\"{$i}\">{$i}</a>", $constant);





11:12 am on Jul 8, 2009 (gmt 0)

5+ Year Member

Sweet, works like an absolute treat!

Seems that maybe I was trying to be too clever with using regex, I didn't even think to try a different method!

Thanks a lot!


1:40 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

I just realized this won't work if the same address occurs more than once because it replaces it twice. i.e.
some text www.someplace.com some other text www.someplace.com will return
some text <a href="<a href="http://www.someplace.com">http://www.someplace.com</a>"><a href="http://www.someplace.com">http://www.someplace.com</a></a> some other text <a href="<a href="http://www.someplace.com">http://www.someplace.com</a>"><a href="http://www.someplace.com">http://www.someplace.com</a></a>.
It returns that because it replaces it once, then the link is there again and it replaces that again. To fix it,
modify the function to this

//now we replace the string
foreach($links as $i){


$links = array_unique($links);

//now we replace the string
foreach($links as $i){

just add the array_unique line. Just an FYI.


Featured Threads

Hot Threads This Week

Hot Threads This Month