Advanced str_replace context problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Advanced str_replace context problem

Post by JAB Creations »

I'm exceptionally close to getting my BB code to XHTML validator/parser to working without a hitch however there is an advanced issue with PHP's str_replace function that I simply can't figure out how to deal with. I've setup a testing area on my site that once I'm absolutely sure it's rock solid I'll post over in the critique forum however I need some help with this issue first please.

The function below is fine when I'm dealing with simple BB tags, however when I need to find and replace three strings everything goes hay-wire. There are two problems with this. First I'm not sure how to only replace the ending bracket of the first BB tag (]). This one small bracket is causing all the chaos for me. :| I'm not sure if there is a way even after reading php.net to force converting only the bracket if it's within say a certain range of the other string parameters. Since it applies to all brackets this opens a hole up to non-BB code bracket such as [i-do-not-exist] converting the ending bracket to the second half of the opening anchor element.

The only remote idea I've had is to somehow go through and replace all the end-brackets in the initial BB tags and then later run another str_replace for those strings later on...but then I'm not sure how to do that as the string *before* the bracket and *after* the bracket is still subjective content!

Thoughts please?

Since this is dealing with BB code I have to disable the BBcode parsing.
________________________

function bb_3_xhtml($text0)
{
$bb1 = array('<', '>');
$xml1 = array('<', '>');


$bb2 = array(
'[b]','[/b]',
'[syntax=php]','[/syntax]',
//'[color=','[/color]',']',
'[i]','[/i]',
'[q]','[/q]',
'[u]','[/u]',
'[quote]', '[/quote]',
//'[list]', '[*]', '[/list]',
);

$xml2 = array(
'<b>','</b>',//'<span class="b">','</span>',
'<code><pre>','</pre></code>',
//'<span style="color: ', '</span>',';">',
'<i>','</i>',//'<span class="i">','</span>',
'<q>','</q>',
'<u>','</u>',//'<span class="u">','</span>',
//'<ul>', '<li>', '</ul>',
"\n<blockquote>", "<blockquote>\n",
);

$bb3 = array('[img]','[/img]');
$xml3 = array('<img alt="" src="', '" />');


$bb4 = array('[size="','[/size]','"]');
$xml4 = array('<span style="font-size: ', '</span>','px;">');


$bb5 = array('[url=','[/url]',']');
$xml5 = array('<a class="icon external" href="', '</a>','" rel="nofollow" tabindex="3">');


$text1 = str_replace($bb1, $xml1, $text0);
$text2 = str_replace($bb2, $xml2, $text1);
$text3 = str_replace($bb3, $xml3, $text2);
$text4 = str_replace($bb4, $xml4, $text3);
$text5 = str_replace($bb5, $xml5, $text4);

$result = bb_4_n2p($text5);
return $result;
}
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Advanced str_replace context problem

Post by JAB Creations »

If at first you don't succeed, blow stuff up! :mrgreen:

Still trying to avoid the use of regular expressions here is what I came up with tonight. I think this will grant me more freedom as far as not constricting how the BBcode should be written. I'll restrict the use of the str_replace function restricted to simple BBcode tags. Any thoughts are welcomed... :)
_______________

$pizza = 'place text [url=http://www.jabcreations.com/1]11[/url] at place [url=http://www.jabcreations.com/2]22[/url] some where [url=http://www.jabcreations.com/3]33[/url] with that';

function bb_tnt($pizza)
{
$p1 = explode("[url",$pizza);

foreach($p1 as $key => &$value)
{
$p2 = explode("]",$value);
$url = explode("=",$p2[0]);
$txt = explode("[",$p2[1]);

if (count($p2)!='1') {$value = '<a class="icon external" href="'.$url[1].'" rel="nofollow" tabindex="3">'.$txt[0].'</a>'.$p2[2];}

}
$pizza = implode($p1, '');

return $pizza;
}


echo bb_tnt($pizza);
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Advanced str_replace context problem

Post by JAB Creations »

There are a few issues with the first iteration of the code (in the second post).

This is going in to the test environment that I'll post later. Suggestions are welcome.

________________________

<?php

$pizza = 'some [color=#f00][size=48]size1[/size][/color] some [size=36][url=http://www.example.com/1]url1[/url][/size] [size=68]where2[/size] some [size=28]where3[/size] ';
//$pizza = 'place text [url=http://www.example.com/1]11[/url] at [color=#f00]place[/color] [url=http://www.example.com/2]22[/url] some [size=48]where[/size] [url=http://www.example.com/3]33[/url] with that';
//$pizza = 'place text [url=http://www.example.com/1]11[/url] at place [url=http://www.example.com/2]22[/url] some where [url=http://www.example.com/3]33[/url] with that';
//$pizza = 'text0 [size=28]size1[/size] [url=http://www.example.com/1]anchor1[/url] text1 [url=http://www.example.com/2]anchor2[/url] text2 [url=http://www.example.com/3]anchor3[/url] text3';

echo '<div>'.$pizza.'</div><br /><br />';

function bb_tnt($pizza)
{
/////////////////////////////////
// URL!
/////////////////////////////////
$p1 = explode("[url",$pizza);

foreach($p1 as $key => &$value)
{
$p2 = explode("[/url]",$value);
$p3 = explode("]",$p2[0]);
$p4 = explode("=",$p3[0]);
//echo '<div>'.$value.'</div>';

if (count($p2)=='2') {$value = '<a class="icon external" href="'.$p4[1].'" rel="nofollow" tabindex="3">'.$p3[1].'</a>'.$p2[1];}
}
$pizza = implode($p1, '');

/////////////////////////////////
// SIZE!
/////////////////////////////////
$p1 = explode("[size",$pizza);

foreach($p1 as $key => &$value)
{
$p2 = explode("[/size]",$value);
$p3 = explode("]",$p2[0]);
$p4 = explode("=",$p3[0]);
//echo '<div>'.$value.'</div>';

if (count($p2)=='2') {$value = '<span style="font-size: '.$p4[1].'px;">'.$p3[1].'</span>'.$p2[1];}
}
$pizza = implode($p1, '');

/////////////////////////////////
// COLOR!
/////////////////////////////////
$p1 = explode("[color",$pizza);

foreach($p1 as $key => &$value)
{
$p2 = explode("[/color]",$value);
$p3 = explode("]",$p2[0]);
$p4 = explode("=",$p3[0]);
//echo '<div>'.$value.'</div>';

if (count($p2)=='2') {$value = '<span style="color: '.$p4[1].';">'.$p3[1].'</span>'.$p2[1];}
}
$pizza = implode($p1, '');

return $pizza;
}

echo "\n\n\n<br /><br />\n".bb_tnt($pizza);
?>
Post Reply