7 Years
Discussion Span
Last Post by martin5211

It could be because you are using the wrong call back for the preg_replace.

Here's how I've done it in the past:

    $articledata=preg_replace("#\[url=([^\]]+)\](.+?)\[/url\]#ies","BuildLinks(array('0'=>2,'1'=>'\\1','2'=>'\\2'))", $articledata);

  function BuildLinks($arr){
    case 1:
        $ret="<a href=mailto:{$arr['1']} rel='extern'>{$arr['2']}</a>";
    case 2:
        $ret="<a href='{$arr['1']}' rel='extern'>{$arr['2']}</a>";
    return $ret;    

Hi all,

i have this bbcode for a [URL] which is not working.
Please let me know whats wrong with it.

// Perform URL Search

$post = preg_replace("/[url]([$URLSearchString]*)[\/url]/", '<a href="javascript:go(\'$1\',\'new\')">$1</a>', $post);

$post = preg_replace("([url\=([$URLSearchString]*)](.+?)[/url])", '<a href="javascript:go(\'$1\',\'new\')">$2</a>', $post);

Thank you.

Edited by Reverend Jim: Fixed formatting

// Perform URL Search

 $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '<a href="javascript:go(\'$1\',\'new\')">$1</a>', $Text);

$Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", '<a href="javascript:go(\'$1\',\'new\')">$2</a>', $Text);

//$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);

// Convert new line chars to html <br /> tags

 $Text = nl2br($Text);

That is because you are using $1 and $2 instead of \\1 and \\2 for the call backs.

$1 and $2 mean nothing.


The problem could also be with your regular expression.

I take it you are trying to make it more dynamic allowing someone to define the regex using the variable $URLSearchString .

Since I can't see what is defined this could be where the problem lies as well. Have you tried to use the regex from my example in place of yours?

//$Text = "[url]http://www.google.com/";
$Text = "fdsfd_shttp://";

$Text = preg_replace("/\[url\]([a-z\:\/\.]*)\[\/url\]/",
					 '<a href="javascript:go(\'${1}\',\'new\')">${1}</a>',

$Text = preg_replace("/\[url\=([a-z\:\/\.]*)\]([a-zA-Z0-9_\:\/]*)\[\/url\]/",
					 '<a href="javascript:go(\'${1}\',\'new\')">${2}</a>',
echo $Text;

Missing braces...


Are you talking about {1} {2} braces above? I have made changes accordingly but it too didnt work.:(


here is my entire code .
pls: see below


 function BBCode($Text)
// Set up the parameters for a URL search string
 $URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";

// Set up the parameters for a MAIL search string
 $MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@";

//Non BB URL Search
//$Text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\" target=\"_blank\" target=\"_new\">\\1://\\2\\3</a>", $Text);

//$Text = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href=\"mailto:\\1\" target=\"_new\">\\1</a>", $Text);

if (substr($Text,0, 7) == "http://"){

$Text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\">\\1://\\2\\3</a>", $Text);

// Convert new line chars to html <br /> tags
 $Text = nl2br($Text);

} else {

// Perform URL Search

 $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '<a href="javascript:go(\'${1}\')">${1}</a>', $Text);

$Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", '<a href="javascript:go(\'${1}\',\'new\')">${2}</a>', $Text);

//$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="${1}" target="_blank">${2}</a>', $Text);

// Convert new line chars to html <br /> tags

 $Text = nl2br($Text);

 // Perform MAIL Search

$Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '<a href="mailto:$1">$1</a>', $Text);
$Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);

 $CodeLayout = '<table width="90%" border="0" align="center" cellpadding="15" cellspacing="0">

 <td class="quotecodeheader"> Code:</td>

 <td class="codebody"  >$1</td>




 $Text = preg_replace("/\[code\](.+?)\[\/code\]/is","$CodeLayout", $Text);

 $QuoteLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">

 <tr> <td class="quotecodeheader"> Quote:</td> </tr>
 <tr> <td class="quotebody">$1</td> </tr>


 $Text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","$QuoteLayout", $Text);
return $Text;


What does fulfil <b></b> tags into <a href="..." code ?

$Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '<a href="javascript<b></b>:go(\'${1}\')">${1}</a>', $Text);

$Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", '<a href="javascript<b></b>:go(\'${1}\',\'new\')">${2}</a>', $Text);

Those tags only displayed when I change to plain text.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.