+ Reply to Thread
Results 1 to 3 of 3

Thread: PHP/RegExp: Escaping forward slash

  1. #1
    tenx23 is offline x10Hosting Member tenx23 is an unknown quantity at this point
    Join Date
    Mar 2011
    Posts
    25

    PHP/RegExp: Escaping forward slash

    Hello,

    I am having trouble with my Regular Expression I'm using to make a BBcode system.

    I can't seem to be able to escape the forward slash.

    Here is the preg pattern I have:

    Code:
    /\[a\](.*?)\[\/a\]/i
    It doesn't like the \/a and comes up with unknown modifier 'A'.


    Help please!

    Thanks,
    tenx23

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP/RegExp: Escaping forward slash

    PHP Code:
    <?php 

    $patt 
    '/\[a\](.*?)\[\/a\]/i' ;

    $hay 'foo and [A]something here[/a] past the match';

    $res preg_match_all$patt$hay$matches ) ;

    echo 
    $matches][0] ;
    echo 
    "   <br />\n" ;
    echo 
    $matches][0] ;
    Works as expected with no errors.

    Posting just a snippet as you did removes it from the context where there might be other errors. It is always best to post all of the relevant code.
    Last edited by descalzo; 08-03-2011 at 06:55 PM.
    Nothing is always absolutely so.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP/RegExp: Escaping forward slash

    Note that you don't need to use forward slashes as delimiters. Pick a different delimiter and you don't need to escape the slash.

    PHP Code:
    preg_match('%\[a\](.*?)\[/a\]%i'''''
    However, regular expressions aren't terribly suited for parsing context-free languages such as BBCode. If someone were to nest [a] elements, it will break your parser. It's not as bad as parsing HTML with regexes, but REs still aren't the correct tool.

    Unless you're doing this for your own education, use an existing BBCode library, such as PHP's built-in bbcode extension. Otherwise, you'll find yourself making more mistakes (such as parsing with regexes), which is fine when learning but not for production systems. If you need features not present in existing libraries, you can extend one rather than starting from scratch.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. I do not coming forward
    By dantler in forum Free Hosting
    Replies: 2
    Last Post: 05-02-2010, 08:38 PM
  2. Forward all email?
    By liammk in forum Programming Help
    Replies: 3
    Last Post: 04-04-2010, 04:57 PM
  3. Auto character escaping?
    By xevell in forum Free Hosting
    Replies: 2
    Last Post: 01-16-2010, 12:36 AM
  4. How to forward URL
    By ranjeet in forum Free Hosting
    Replies: 2
    Last Post: 01-01-2009, 04:04 AM
  5. Looking Forward
    By kewpie in forum Introductions
    Replies: 3
    Last Post: 05-19-2008, 12:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers