homepage Welcome to WebmasterWorld Guest from 54.224.53.192
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
preg replace urls in RSS feed
Locating and replacing urls as anchors using SimplePie
pmells

5+ Year Member



 
Msg#: 3947660 posted 1:59 pm on Jul 7, 2009 (gmt 0)

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

 

andrewsmd

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3947660 posted 4:10 pm on Jul 7, 2009 (gmt 0)

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:");

}//while

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

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

}//foreach

echo(($constant));

}//createLink

pmells

5+ Year Member



 
Msg#: 3947660 posted 11:12 am on Jul 8, 2009 (gmt 0)

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!

andrewsmd

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3947660 posted 1:40 pm on Jul 8, 2009 (gmt 0)

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
Change:
}//while

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

to:
}//while

$links = array_unique($links);

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

just add the array_unique line. Just an FYI.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved