Jump to content

preg_replace on everything except


tiki

Recommended Posts

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);

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

  Quote
this is <strong>BOLD</strong><pre>so is <strong>THIS</strong></pre>

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;
?>

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)

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.