Jump to content

[SOLVED] preg_replace doesn't replace all strings


fert

Recommended Posts

I have this code
[code]
function bbcode($comment)
{
$bb=array("'\[b\](.*?)\[/b\]'","'\[i\](.*?)\[/i\]'","'\[u\](.*?)\[/u\]'","'\[url=(.*?)\](.*?)\[/url\]'","'\[color=(.*?)\](.*?)\[/color\]'","'\[size=(.*?)\](.*?)\[/size\]'","'\[img\](.*?)\[/img\]'","'\[quote\](.*?)\[/quote\]'","'\[spoiler\](.*?)\[/spoiler\]'","'\[code\](.*?)\[/code\]'","'\[center\](.*?)\[/center\]'","'\[sub\](.*?)\[/sub\]'","'\[sup\](.*?)\[/sub\]'");
$replace=array("<b>$1</b>","<i>$1</i>","<u>$1</u>","<a href=\"$1\">$2</a>","<font color=\"$1\">$2</font>","<font size=\"$1\">$2</font>","<a href=\"$1\"><img scr=\"$1\" alt=\"Posted image\"></a>","<textarea readonly rows=\"5\" cols=\"40\">Quote:\n$1</textarea>","<font color=\"#FFFFFF\">$1</font>","<textarea rows=\"5\" cols=\"30\" readonly>Code:\n$1</textarea>","<p align=\"center\">$1</p>","<sub>$1</sub>","<sup>$1</sup>");

$comment=preg_replace($bb,$replace,$comment);

$comment=str_replace("[list]","<ul>",$comment);
$comment=str_replace("[*]","<li>",$comment);
$comment=str_replace("[/*]","</li>",$comment);
$comment=str_replace("[list=1]","<ol>",$comment);
$comment=str_replace("[/list]","</ol></ul>",$comment);

return $comment;
}
[/code]
This is a function to replace BBcode in a blog entry, but any tag beyond [ quote] doesn't work and there are no errors produced. any ideas as to why it doesn't work?
Try this slight reformat of $bb & $replace

[code]
  $bb=array('/\[b\](.*?)\[/b\]/','/\[i\](.*?)\[/i\]/','/\[u\](.*?)\[/u\]/','/\[url=(.*?)\](.*?)\[/url\]/','/\[color=(.*?)\](.*?)\[/color\]/','/\[size=(.*?)\](.*?)\[/size\]/','/\[img\](.*?)\[/img\]/','/\[quote\](.*?)\[/quote\]/','/\[spoiler\](.*?)\[/spoiler\]/','/\[code\](.*?)\[/code\]/','/\[center\](.*?)\[/center\]/','/\[sub\](.*?)\[/sub\]/','/\[sup\](.*?)\[/sup\]/');
  $replace=array("<b>$1</b>","<i>${0}</i>","<u>${0}</u>","<a href=\"${0}\">${1}</a>","<font color=\"${0}\">${1}</font>","<font size=\"${0}\">${1}</font>","<a href=\"${0}\"><img scr=\"${0}\" alt=\"Posted image\"></a>","<textarea readonly rows=\"5\" cols=\"40\">Quote:\n${0}</textarea>","<font color=\"#FFFFFF\">${0}</font>","<textarea rows=\"5\" cols=\"30\" readonly>Code:\n${0}</textarea>","<p align=\"center\">${0}</p>","<sub>${0}</sub>","<sup>${0}</sup>");

[/code]
Oops.  Try replacing all the outer double quotes (") with single quotes (') in the $replace array

[code]
  $replace=array('<b>${0}</b>>','<i>${0}</i>','<u>${0}</u>','<a href="${0}">${1}</a>','<font color="${0}">${1}</font>','<font size="${0}">${1}</font>','<a href="${0}"><img scr="${0}" alt="Posted image"></a>','<textarea readonly rows="5" cols="40">Quote:${0}</textarea>','<font color="#FFFFFF">${0}</font>','<textarea rows="5" cols="30" readonly>Code:${0}</textarea>','<p align="center">${0}</p>','<sub>${0}</sub>','<sup>${0}</sup>');

[/code]

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.