Jump to content

[SOLVED] [url] to <a>


chocopi

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/54894-solved-url-to/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/54894-solved-url-to/#findComment-271513
Share on other sites

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

?>

Link to comment
https://forums.phpfreaks.com/topic/54894-solved-url-to/#findComment-271830
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.