Page 1 of 1

Pseudo phpbb code... nested commands?

Posted: Tue Jul 17, 2007 11:48 am
by Chalks
I'm building a translate.php page, where it simply takes the string input, finds all phpbb code, and replaces it with valid html. So far, I have not even attempted to make sure that everything has a closing tag, and I probably won't worry about that (because it's an administration panel, not a forum type posting thing). However, I am running into trouble with my "url" code. The problem is if I nest commands like bold inside my string:

Code: Select all

[url][b]website.com[/b][/url]

evaluates to

<a href="<b>website.com</b>"><b>website.com</b></a>
I know where my error is in my regex, but I'm not sure how to fix it:

Code: Select all

<?php
function translate($str)
{
$pat[0] = "/[\[]n[\]]/i";
$pat[1] = "/[\[]b[\]]/i";
$pat[2] = "/[\[]i[\]]/i";
$pat[3] = "/[\[]u[\]]/i";
$pat[4] = "/[\[][\/]b[\]]/i";
$pat[5] = "/[\[][\/]i[\]]/i";
$pat[6] = "/[\[][\/]u[\]]/i";
$pat[7] = "/[\[]url[=][&]quot[;](.*)[&]quot[;][\]]/i";
$pat[8] = "/[\[]url[\]](.*)[\[][\/]url[\]]/i";  // the problem is the .*... I need it to pull everything BUT phpbb codes.  How?
$pat[9] = "/[\[][\/]url[\]]/i";

$rep[0] = "<br />";
$rep[1] = "<b>";
$rep[2] = "<i>";
$rep[3] = "<u>";
$rep[4] = "</b>";
$rep[5] = "</i>";
$rep[6] = "</u>";
$rep[7] = "<a href=\"$1\">";
$rep[8] = "<a href=\"$1\">$1</a>";
$rep[9] = "</a>";

$str = preg_replace($pat, $rep, $str);
return $str;
}

incidently, the reason I'm looking for """ instead of an " is because I just changed all potentially dangerous symbols (<, >, ", ', etc) into the html equivilent.

Posted: Fri Jul 20, 2007 5:12 am
by MalikBB
Bad idea to directly convert BB-codes into HTML-codes through regexp-s.

So, about your reg-exps:

text between tags [...] and [/...] could not content any "[" and "]" symbols => so you can convert to "<...>text</...>"

(.*) replace with ([^\[|\]])

Posted: Fri Jul 20, 2007 7:51 am
by Chalks
MalikBB wrote:Bad idea to directly convert BB-codes into HTML-codes through regexp-s.
Any particular reason? I mean, this way I have control over which tags are allowed. I'm not sure I'm understanding your comment correctly.
MalikBB wrote:So, about your reg-exps:

text between tags [...] and [/...] could not content any "[" and "]" symbols => so you can convert to "<...>text</...>"

(.*) replace with ([^\[|\]])
Ah, that would work perfectly. Thanks!