nezbo Posted April 22, 2008 Share Posted April 22, 2008 dose anyone know of a function that can get a string and change [ u r l ] www.google.co.uk [/ u r l ] when it is typed in a text box. like the one used on this page. i have a string that i want to pass it, but i am just looking for the function. Cheers Quote Link to comment Share on other sites More sharing options...
thebadbad Posted April 22, 2008 Share Posted April 22, 2008 Maybe have a look here: http://www.phpfreaks.com/forums/index.php/topic,123171 Quote Link to comment Share on other sites More sharing options...
nezbo Posted April 22, 2008 Author Share Posted April 22, 2008 Cheers for that. but i need it to send it the other way. that is just disabling urls and not changing them in to links. so i need to change it from [ url]www.test.co.uk[ /url] to <a href="www.test.co.uk">www.text.co.uk</a> hope this makes more sence... Maybe have a look here: http://www.phpfreaks.com/forums/index.php/topic,123171 Quote Link to comment Share on other sites More sharing options...
nezbo Posted April 22, 2008 Author Share Posted April 22, 2008 i have started this function and i cant get it to work properly . i can get it to either get the first URL or the second, but not both. testing function echo bb2html("teis is some test text [url]www.google.co.uk[/url] second [url]www.can.co.uk[/url]"); function bb2html($text) { $splitURL = split("\[url\]", $text); $countURL = count($splitURL); for ($i=0; $i < $countURL; $i++) { $clipURL = split("\[/url\]", $splitURL[$i]); $bbcode = "[url]" . $clipURL[0] . "[/url]"; $htmlcode = "<a target='_blank' href='http://" . $clipURL[0] . "'>" . $clipURL[0] . "</a>"; $newtext = str_replace($bbcode, $htmlcode, $text); } $newtext = nl2br($newtext);//second pass return $newtext; } Quote Link to comment Share on other sites More sharing options...
retro Posted April 22, 2008 Share Posted April 22, 2008 so i need to change it from [ url]www.test.co.uk[ /url] to <a href="www.test.co.uk">www.text.co.uk</a> That is what BBCode does. This is a built-in function in most PHP boards, so you could look at the source code for an open source board such as phpBB. I think the link given to you by thebadbad should contain a solution, though. Quote Link to comment Share on other sites More sharing options...
retro Posted April 22, 2008 Share Posted April 22, 2008 Using the example given on that referred thread, the following works: function bbcode($txt) { // bbcodes $bbcodes = array( "|\[b\](.+?)\[/b\]|is", "|\[u\](.+?)\[/u\]|is", "|\[i\](.+?)\[/i\]|is", "|\[url\](.+?)\[/url\]|is" ); // html $replace = array( "<strong>$1</strong>", "<u>$1</u>", "<em>$1</em>", '<a href="$1">$1</a>' ); $txt = preg_replace($bbcodes, $replace, $txt); return nl2br($txt); } $str = "hey go to http://www.phpfreaks.com!"; $str = bbcode($str); echo $str; I left in the other bbcode for formatting, but you could of course simplify and remove this. There is one problem - if you don't start your url with http:// then it messes up. For example, if you omit the http:// in the above sample, it would link to http://www.yoursite.com/www.phpfreaks.com ...if you can find a workaround for this, you have some workable code Quote Link to comment Share on other sites More sharing options...
nezbo Posted April 22, 2008 Author Share Posted April 22, 2008 cheers for the help, but i have worked out a way of doing it. here is me functions all you have to do in the text box is : [ url]www.mysite.co.uk;My Site[/ url] function br2nl($str) { $br2nl = "<br />"; $nlnl = ""; $newstr = str_replace($br2nl, $nlnl, $str); return $newstr; } function bb2html($text) { $splitURL = split("\[url\]", $text); $countURL = count($splitURL); $newtext = $text; for ($i=0; $i < $countURL; $i++) { $clipURL = split("\[/url\]", $splitURL[$i]); $newClipURL = split(";", $clipURL[0]); $bbcode = "[url]" . $clipURL[0] . "[/url]"; $htmlcode = '<a href="http://' . $newClipURL[0] . '" target="_blank">' . $newClipURL[1] . '</a>'; $newtext = str_replace($bbcode, $htmlcode, $newtext); } $newtext = nl2br($newtext);//second pass return $newtext; } function html2bb($text) { $splitURL = split("<a href=\"http:\/\/", $text); $countURL = count($splitURL); $newtext = $text; for ($i=0; $i < $countURL; $i++) { $clipURL = split('</a>', $splitURL[$i]); $newClipURL = split('" target="_blank">', $clipURL[0]); $bbcode = '<a href="http://' . $newClipURL[0] . '" target="_blank">' . $newClipURL[1] . '</a>'; $htmlcode = "[url]" . $newClipURL[0] . ";" . $newClipURL[1] . "[/url]"; $newtext = str_replace($bbcode, $htmlcode, $newtext); } $newtext = br2nl($newtext);//second pass return $newtext; } sorry i couldnt get my head around the other way. Cheers Neil Quote Link to comment Share on other sites More sharing options...
retro Posted April 22, 2008 Share Posted April 22, 2008 I'm glad you found a way that works! Ahh, sorry! I seem to have been a complete idiot and quoted the text, instead of using code! This ballsed up my code! Quote Link to comment 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.