Jump to content

preg_replace on everything except


tiki

Recommended Posts

So I have this BB code parsing class I wrote that works perfectly. Just one thing I cant figure out. I want all tags ([ b], etc) to be parsed EXCEPT if they are within [ code] tags. Any help would be greatly appreciated.

Link to comment
Share on other sites

I havent even begun how to do this part, since im completely lost. Im really good at regex, just never done something like this. Im assuming it will be a preg_match_all.

 

But heres a snippet of the code, that this needs to be applied to:

 

private $markup = array(

'/\[b\](.*?)\[\/b\]/is' => '<strong>$1</strong>',

'/\[i\](.*?)\[\/i\]/is' => '<em>$1</em>',

'/\[u\](.*?)\[\/u\]/is' => '<span style="text-decoration: underline">$1</span>',

'/\[align=(left|center|right)\](.*?)\[\/align\]/is' => '<div style="text-align: $1">$2</div>',

'/\{6}|[a-z]+)\](.*?)\[\/color\]/is' => '<span style="color: $1">$2</span>',

'/\(.*?)\[\/font\]/is' => '<span style="font-family: \'$1\', sans-serif;">$2</span>',

'/\[h([1-6]{1})\](.*?)\[\/h([1-6]{1})\]/is' => '<h$1>$2</h$3>',

'/\{1})?[0-9]{1})\](.*?)\[\/size\]/is' => '<span style="font-size: $1px">$2</span>',

'/\[code\](.*?)\[\/code\]/is' => '<pre>$1</pre>',

'/\[sub\](.*?)\[\/sub\]/is' => '<sub>$1</sub>',

'/\[sup\](.*?)\[\/sup\]/is' => '<sup>$1</sup>'

);

 

// Later in the code

$string = preg_replace(array_keys($this->markup), $this->markup, $string);

Link to comment
Share on other sites

Nothing wrong with that code

 

my test

<?php
$markup = array(
      '/\[b\](.*?)\[\/b\]/is'                              => '<strong>$1</strong>',
      '/\[i\](.*?)\[\/i\]/is'                              => '<em>$1</em>',
      '/\[u\](.*?)\[\/u\]/is'                              => '<span style="text-decoration: underline">$1</span>',
      '/\[align=(left|center|right)\](.*?)\[\/align\]/is'         => '<div style="text-align: $1">$2</div>',
      '/\[color=(#[0-9a-fA-F]{6}|[a-z]+)\](.*?)\[\/color\]/is'   => '<span style="color: $1">$2</span>',
      '/\[font=\"(.*?)\"\](.*?)\[\/font\]/is'                  => '<span style="font-family: \'$1\', sans-serif;">$2</span>',
      '/\[h([1-6]{1})\](.*?)\[\/h([1-6]{1})\]/is'               => '<h$1>$2</h$3>',
      '/\[size=((?:[1-2]{1})?[0-9]{1})\](.*?)\[\/size\]/is'      => '<span style="font-size: $1px">$2</span>',
      '/\[code\](.*?)\[\/code\]/is'                        => '<pre>$1</pre>',
      '/\[sub\](.*?)\[\/sub\]/is'                           => '<sub>$1</sub>',
      '/\[sup\](.*?)\[\/sup\]/is'                           => '<sup>$1</sup>'
   );

   
$string = "this is [b]BOLD[/b][code ]so is [b]THIS[/b][/code ]"; //code without the spaces
// Later in the code
$string = preg_replace(array_keys($markup), $markup, $string);
echo $string;

?>

results

this is <strong>BOLD</strong><pre>so is <strong>THIS</strong></pre>
Link to comment
Share on other sites

I see..

thier is probably a few ways but heres a simple one

basically i grab the string inside the

[code ] tag and encode it then do the bbreplace then decode the code tag's

<?php
$markup = array(
      '/\[b\](.*?)\[\/b\]/is'                              => '<strong>$1</strong>',
      '/\[i\](.*?)\[\/i\]/is'                              => '<em>$1</em>',
      '/\[u\](.*?)\[\/u\]/is'                              => '<span style="text-decoration: underline">$1</span>',
      '/\[align=(left|center|right)\](.*?)\[\/align\]/is'         => '<div style="text-align: $1">$2</div>',
      '/\[color=(#[0-9a-fA-F]{6}|[a-z]+)\](.*?)\[\/color\]/is'   => '<span style="color: $1">$2</span>',
      '/\[font=\"(.*?)\"\](.*?)\[\/font\]/is'                  => '<span style="font-family: \'$1\', sans-serif;">$2</span>',
      '/\[h([1-6]{1})\](.*?)\[\/h([1-6]{1})\]/is'               => '<h$1>$2</h$3>',
      '/\[size=((?:[1-2]{1})?[0-9]{1})\](.*?)\[\/size\]/is'      => '<span style="font-size: $1px">$2</span>',
      '/\[sub\](.*?)\[\/sub\]/is'                           => '<sub>$1</sub>',
      '/\[sup\](.*?)\[\/sup\]/is'                           => '<sup>$1</sup>'
   );

//remove the first space from code
$string = "this is [b]BOLD[/b][ code]so is [b]THIS[/b][/ code]"; //code without the spaces

//remove the first space from code
$string = preg_replace_callback('/\[ code\](.*?)\[\ /code\]/is',create_function('$string','return "[newcode]".base64_encode($string[1])."[/newcode]";'),$string);
$string = preg_replace(array_keys($markup), $markup, $string);
$string = preg_replace_callback('/\[newcode\](.*?)\[\/newcode\]/is',create_function('$string','return "<pre>".base64_decode($string[1])."</pre>";'),$string);

echo $string;
?>

Link to comment
Share on other sites

Actually there is but its a lot more coding.

I have something like that which is approx 90% complete, but had to stall on the development of the code.

 

wut u end up doing is parsing the bbcode tags individually and build up a stack.

and when u encounter a end tag, u pop it off the stack

and process the tags.

 

nice thing, if done using a stack, is that it dusnt get confused about which are the matching end tags.

FIFO stack.

 

but it is a lot of extra coding, because you are building this stack.

Than a new problem arised, wut to do with mismatched tags.

 

Anyways good luck

if u want to proceed in building a stack based bbcode parser, I can help (More like point u in right direction than providing code)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.