Jump to content

[ u r l ] Function


nezbo

Recommended Posts

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...

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/102295-u-r-l-function/#findComment-523779
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/102295-u-r-l-function/#findComment-523796
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/102295-u-r-l-function/#findComment-523798
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/102295-u-r-l-function/#findComment-523812
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/102295-u-r-l-function/#findComment-523842
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.