Jump to content

[SOLVED] BBcode Parsing Problems (preg_replace)


Grego

Recommended Posts

I've stored all my BBcode terms in a MySQL database containing, amongst other things, their patterns and replacements. For example:

 

Pattern: /\[b\](*.?)\[\/b\]/is

Replacement: <strong>$1</strong>

 

There are five of these records: b, i, u, url, img

 

I then have a piece of code which draws out the patterns and replacements and puts them in an array. I use preg_replace to replace all the patterns with their respective replacements. For some reason, I'm getting an error saying: "Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 6 in /home/commande/public_html/gregoland/functions_bbcode.php on line 20". I've tried googling this but have only returned examples of it rather than solutions. Does anybody know what's wrong and how I can get rid of this error?

 

This is the code I'm using:

 

  $bb_codes_q = $db->execute('SELECT * FROM `bbcode`');
  $bb_patterns = array();
  $bb_replaces = array();

    while($bb_codes = $bb_codes_q->fetchrow()){
    array_push($bb_patterns,$bb_codes['pattern']);
    array_push($bb_replaces,$bb_codes['replace']);
    }

  $bb_text = preg_replace($bb_patterns,$bb_replaces,$bb_text);
  return $bb_text;

Link to comment
Share on other sites

I've tried that and I can't see anything wrong with either array. I tried too to replace the arrays with an empty "array()". When I replaced $bbcode_patterns, I found that the error had gone. So I've narrowed it down to that variable being wrong. Here is the print_r for it:

Array
(
    [0] => /\[b\](*.?)\[\/b\]/is
    [1] => /\[i\](*.?)\[\/i\]/is
    [2] => /\[u\](*.?)\[\/u\]/is
    [3] => /\[url\=(*.?)\](*.?)\[\/url\]/is
    [4] => /\[img\](*.?)\[\/img\]/is
)

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.