Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8

Thread: Odd PHP error

  1. #1
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    I've coded a BBcode parser for a site I am involved with. There is nothing wrong with that, as far as I know, but it keeps spitting out an odd error:

    Warning: preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset 23 in **directory** on line 55
    Everything seems to load fine - I think, and I've checked the code and it also seems fine. Any ideas as to what may be causing this odd error?

    EDIT: Sorry about the title! I was too busy copying and pasting the post and forgot about it!

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Don't suppose you could change the thread topic? ;-)

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Make sure you're using [phpfunction]preg_quote[/phpfunction].
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  5. #4
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    I tried using preg_quote, and it completely garbled the output. Here's the script, anyway:

    PHP Code:
    <?php

    // Press Start Online SimpleCode translator


        // For backwards compatibility, remove trailing slashes from past articles
        
    $original_string str_replace("/]","]",$original_string);
        
    $original_string str_replace("","'",$original_string);
        
        
    // Search for the following SimpleCodes
        
    $bbcodes = array(
                        
    "'\[b\](.*?)\[/b\]'i",
                        
    "'\[u\](.*?)\[/u\]'i",
                        
    "'\[i\](.*?)\[/i\]'i",
                        
    "'\[img\](.*?)\[/img\]'is",
                          
    "'\[url\](.*?)\[/url\]'is",
                          
    "'\[url=(.*?)\](.*?)\[/url\]'is",
                        
    "'\[il=(.*?)\](.*?)\[/il\]'i",
                        
    "'(\[1img\](.*?)\[/1img\]'is",
                        
    "'\[2img\](.*?)\[/2img\]'is",
                        
    "'\[3img\](.*?)\[/3img\]'is",
                        
    "'\[1img\](.*?)\[endimg\]'is",
                        
    "'\[2img\](.*?)\[endimg\]'is",
                        
    "'\[3img\](.*?)\[endimg\]'is",
                        
    "'\[quote\](.*?)\[/quote\]'is",
                        
    "'\[h2\](.*?)\[/h2\]'i",
                        
    "'\[h1\](.*?)\[/h1\]'i",
                        
    "'\[fl=(.*?)\](.*?)\[/fl\]'i",
                        
    "'\[hr\]'",
                        
    "'\[p\]'"
                        
    );

        
    // Replace the SimpleCodes with XHTML
        
    $replace = array( 
                        
    "<strong>\\1</strong>",
                         
    "<u>\\1</u>",
                         
    "<em>\\1</em>",
                         
    "<img src=\"\\1\" alt=\"image\" />",
                         
    "<a href=\"\\1\" title=\"\\1\">\\1</a>",
                         
    "<a href=\"\\1\" title=\"\\1\">\\2</a>",
                         
    "<a href=\"image.php?id=\\1\" title='click for large version'>\\2</a>",
                         
    "<div id='oneimage'>\\1</div><div id=\"endfloat\"></div>",
                         
    "<div id='twoimages'>\\1</div><div id=\"endfloat\"></div>",
                         
    "<div id='threeimages'>\\1</div><div id=\"endfloat\"></div>",
                         
    "<div id='oneimage'>\\1</div><div id=\"endfloat\"></div>",
                         
    "<div id='twoimages'>\\1</div><div id=\"endfloat\"></div>",
                         
    "<div id='threeimages'>\\1</div><div id=\"endfloat\"></div>",
                         
    "<div id='quote_box'>\\1</div>",
                         
    "<h2>\\1</h2>",
                         
    "<h1>\\1</h1>",
                         
    "<div id='img_\\1'>\\2</div>",
                         
    "<div id=\"hrfix\"><div class=\"sub\"> </div><hr size=\"1\" height=\"1\" width=\"100%\"><div class=\"sub\"> </div></div>",
                         
    "<br /><br />"                    
                        
    );
        
        
    $new_string preg_replace($bbcodes,$replace,$original_string);


    ?>
    The way this works is that I include it into another file, after the variable $original_string has been declared, and before I echo $new_string. Just so I don't have to edit any other files with it all in. Repeated bits in the array are for backwards compatibility with the existing database entries, which were worked around errors in the previous bbcode script.

  6. #5
    Junior Member MikeWire's Avatar
    Join Date
    Jan 2007
    Posts
    4
    Member #
    14728
    missing ) at offset 23 in **directory** on line 55

    This tells me that you have a ")" missing in your 23rd offset in your array. Line 55 is at the end of your array - so a string is being pushed into your array with a "(" and then PHP pukes when it it doesn't see a closing ")" - not sure it thats helps, or if I am just stating the obvious...

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Code:
    "'([1img](.*?)[/1img]'is",
    Look for the missing closing paren in the pattern ;-)

  8. #7
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    I'm wondering why you're splitting the 1img stuff.... can't you just merge them all into one regexp?

    EDIT: Btw, you shouldn't use IDs for things that might be multiple. Unless of course this has to do with your CMS.
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  9. #8
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    Oh so that was it. Hehe, would never have spotted that.

    The 1img/2img/3img thing is related to how the site works. I've left it that way so the staff don't have to adjust to something new, and so all the past articles still format correctly.

    The ID thing was the past administrator's error. I'll be sorting that out once I've got to grips with how he's coded the site. I'll be giving it a standards workout, anyway.

    Thanks for the help guys.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 08:44 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com