chocopi Posted June 9, 2007 Share Posted June 9, 2007 How do you go about changing a url in the form of [url=http://www.somesite.com]Link[/url] to <a href="http://www.somesite.com">Link</a> This would be for some sort of BB code from a textarea I've been messing around with it for some time and i really cant figure it out Thanks, ~ Chocopi By the way, i know this should be in regex, but there is hardly ever anyone in there Quote Link to comment https://forums.phpfreaks.com/topic/54894-solved-url-to/ Share on other sites More sharing options...
Full-Demon Posted June 9, 2007 Share Posted June 9, 2007 Perhaps something like this: You go through the text per character, if the char is a '[' and the following chars are 'url= than you output '<a href="'. Than you output the link till you get the char ]', when you find that char you output '">', the text of the link, and finnaly the </a> Or you could do something with eregi_replace...but you best (IMO) make a function of the above. Full-D Quote Link to comment https://forums.phpfreaks.com/topic/54894-solved-url-to/#findComment-271513 Share on other sites More sharing options...
cooldude832 Posted June 9, 2007 Share Posted June 9, 2007 can the pseudo tag have any other flags or formatting to it, if not find first = and trim to it then trim to the first ] since a ] isn't a valid character in a href= Quote Link to comment https://forums.phpfreaks.com/topic/54894-solved-url-to/#findComment-271515 Share on other sites More sharing options...
chocopi Posted June 10, 2007 Author Share Posted June 10, 2007 well yea i understand how to do it, but i dont know the code. At the moment i have splits and replaces but its too confusing and doesnt work. Just to show you, this is what i have so far: <?php list($text1,$url) = explode('[url=',$text); list($url,$text2) = explode('[/url]',$url); $length = strlen($text1); $length = $length - 5; $text1 = substr($text1, 1, $length); list($url3,$name) = explode(']',$url); $url = str_replace(']','\">',$name); $url3 = $url3.''.$url; $url1 = '<a href=\"'.$url3."".$name.'</a>'; ?> However, i did find this code, so all i need is an efficient way to find the [url][/url] <?php $url = '[url=http://www.foo.org]The link[/url]'; $text = preg_replace("/\[url=(\W?)(.*?)(\W?)\](.*?)\[\/url\]/", '<a href="$2">$4</a>', $url); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54894-solved-url-to/#findComment-271830 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.