Right now I\'m having issues replacing strings that already come out from preg_match. Lets say I have bbcode of [b]bla[/b]
, I have this part working with replacing
Because you are never going to be able to fully trust user data AND because bbcode is just as vulnerable as html to incorrect parsing by regex, you will never be 100% confident that this method will work. Non-quote tags can just as easily be replaced by a non-regex method, so I am eliminating the pattern convolution by segmenting the logic.
I am implementing a recursive pattern for quote tags (assuming everything will be balanced) and using your do-while()
technique -- I think this is the best approach. This will effectively work from outer quote inward on each iteration (while $count
is positive).
Code: (Demo)
function bbcodequote2html($matches){
$text=(isset($matches[2])?$matches[2]:''); // avoid Notices
if(isset($matches[1]) && ctype_digit($matches[1])){
$TPID = "#{$matches[1]}"; // GetThreadPoster($match[1]);
$TPUN = "#{$matches[1]}"; // GetUsernameS($TPID);
$quotee="
- $TPUN";
}else{
$quotee=''; // no id value or id is non-numeric default to empty string
}
return "$text$quotee";
}
$bbcode=<<','','',''],
$bbcode
);
$tabs="\t";
do{
$converted=preg_replace_callback('~\[quote(?:=(.+?))?]((?:(?R)|.*?)+)\[/quote]~is','bbcodequote2html',$converted,-1,$count);
}while($count);
echo $converted;
It is difficult for me to display the output in a fashion that is easy to read. You may be best served to run my code on your server and check that the results render as desired.
Output:
Outer Quotebold nested bold
italic nested italicInner Quote 1: (no id)
Inner Quote 2Inner Quote 3
- #1 still inner quote 2 Inner Quote 4 end of inner quote 2
- #2