Jump to content

[ u r l ] Function


nezbo

Recommended Posts

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

Link to comment
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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