Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    58

    Help with some regex


    Basically i am trying to do a quote replacement bit on a script like how bbcode works.

    Basically i want to replace anything inbetween quote bbcode markers into html.

    What is a good regex to replace anything, i am pretty new at regex.

    Would "(.*)" be a good one to use?
    Last edited by liamdawe; January 13th, 2009 at 05:33 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,513
    Rep Power
    1424
    If you want nested quote tags, the problem isn't solvable using regular expressions. If you don't want or need them,
    Code:
    #\[quote\](.*?)\[/quote\]#si
    should suffice.

    Btw: There is a regular expression subforum here at devshed's
    Last edited by JClasen; January 13th, 2009 at 07:22 PM.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    58
    Sorry didn't notice the subforum if anyone can move that would be helpful .

    What exactly will that do? Just match whatever is between the quote bbcode tags?
  6. #4
  7. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by liamdawe
    Sorry didn't notice the subforum if anyone can move that would be helpful .
    It already has.

    Originally Posted by liamdawe
    What exactly will that do? Just match whatever is between the quote bbcode tags?
    Try and see what it does.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    58
    Okay i got it now i am using this:
    PHP Code:
    $post preg_replace('#\[quote\](.*?)\[/quote\]#mi',"<div class=\"container\"><div class=\"box\">{$lang['quoting']}</div><div class=\"box2\">$1</div></div>",$post); 
    What would be the best way to reverse that?
  10. #6
  11. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by liamdawe
    Okay i got it now i am using this:
    PHP Code:
    $post preg_replace('#\[quote\](.*?)\[/quote\]#mi',"<div class=\"container\"><div class=\"box\">{$lang['quoting']}</div><div class=\"box2\">$1</div></div>",$post); 
    What would be the best way to reverse that?
    Comon, show a bit of initiative! You've been shown how to do it in a certain way, try some things yourself before asking here again. You learn a lot by trial and error. When after some time you don't succeed, post what you have tried here and I (or someone else) will probably be able to tell you the error in your logic.
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    58
    I have tried it hence posting here asking for help, i tried this which does nothing:
    PHP Code:
    $edit preg_replace("#\<div class=\"container\"\>\<div class=\"box\"\>{$lang['quoting']}\</div\>\<div class=\"box2\"\>(.*?)\</div\>\</div\>#m",'[quote]$1[/quote]',$edit); 
    Don't assume people just haven't bother trying :P
  14. #8
  15. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by liamdawe
    I have tried it hence posting here asking for help, i tried this which does nothing:
    PHP Code:
    $edit preg_replace("#\<div class=\"container\"\>\<div class=\"box\"\>{$lang['quoting']}\</div\>\<div class=\"box2\"\>(.*?)\</div\>\</div\>#m",'',$edit); 
    You don't need to escape the < and > characters in your regex, but you DO need to escape $, [ and {.

    Originally Posted by liamdawe
    Don't assume people just haven't bother trying :P
    Sadly, this is often the case when people don't post any extra information. So, I will keep assuming that people who don't do so have not tried an awful lot themselves.

    Note that it is far more beneficial when posting the things you have tried because then you get comments about what you did wrong and learn far more from it. It also shows that you put in some effort to give a detailed explanation of your problem problem instead of just asking: "how to do X?".
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    58
    What you say is true and the posting yeah sorry.

    I have a cheat sheet which says < and > are metacharacters that need to be escaped -> http://ult-tex.net/info/perl/regular_expressions_cheat_sheet.png

    Have now tried this:
    PHP Code:
    $edit preg_replace("#<div class=\"container\"><div class=\"box\">\{\$lang\['quoting'\]\}</div><div class=\"box2\">(.*?)</div></div>#m",'\[quote\]$1\[/quote\]',$edit); 
    Still does nothing
  18. #10
  19. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by liamdawe
    What you say is true and the posting yeah sorry.

    I have a cheat sheet which says < and > are metacharacters that need to be escaped -> http://ult-tex.net/info/perl/regular_expressions_cheat_sheet.png
    The cheat sheet is wrong. Granted, the < can be a meta character inside look around assertions, but in general, it is no meta character. It would be a bit like saying that the hyphen is a meta character because inside c character set it has a special "range" meaning: "[a-z]" -> matches all lowercase letters from a, b, c, .. z.

    Originally Posted by liamdawe
    Have now tried this:
    PHP Code:
    $edit preg_replace("#<div class=\"container\"><div class=\"box\">\{\$lang\['quoting'\]\}</div><div class=\"box2\">(.*?)</div></div>#m",'\[quote\]$1\[/quote\]',$edit); 
    Still does nothing
    Then $edit does not contain a string like:

    <div class="container"><div class="box">{$lang['quoting']}</div><div class="box2">blabla</div></div>

    ; )
  20. #11
  21. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    58
    Well it does as i am testing on this:
    <div class="container"><div class="box">Quote</div><div class="box2">
    test
    </div></div>
    Note that "Quote" is the language set from $lang['quoting'] and works fine when turning it into html so it is not that causing it.
  22. #12
  23. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    58
    I now have it working with this:
    PHP Code:
    $edit preg_replace("#\<div class=\"container\"\>\<div class=\"box\"\>{$lang['quoting']}\</div\>\<div class=\"box2\"\>(.*?)\</div\>\</div\>#s",'[quote]$1[/quote]',$edit); 
    1. Because of the way i parsed it before it was a single line of html code (s needed at the end not m).
    2. Don't escape characters on the second bit of the preg_replace.
    3. I didn't need to touch "{$lang['quoting']}"
    4. I do need to escape double quotes as double quotes surround my regex to parse "{$lang['quoting']}".

    Edit > removed one which was wrong.

    Comments on this post

    • prometheuzz disagrees : Point 2 is wrong. As mentiond many times now.
    Last edited by liamdawe; January 15th, 2009 at 11:31 AM.
  24. #13
  25. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by liamdawe
    Well it does as i am testing on this:

    Note that "Quote" is the language set from $lang['quoting'] and works fine when turning it into html so it is not that causing it.
    This works like a charm for me:

    PHP Code:
    echo preg_replace(
      
    '#<div class="container"><div class="box">\{\$lang\[\'quoting\']}</div><div class="box2">(.*?)</div></div>#i'
      
    '[quote]$1[/quote]'
      
    '<div class="container"><div class="box">{$lang[\'quoting\']}</div><div class="box2">blabla</div></div>'
    ); 
    Note that you also don't need to escape the double quotes since you're using a single quote around your regex pattern and there is no need to escape any other characters than the $ inside your replacement string.

    HTH
    Last edited by prometheuzz; January 15th, 2009 at 10:31 AM.
  26. #14
  27. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by liamdawe
    ...
    2. I did need to escape < and >.
    Really, you don't.
  28. #15
  29. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    634
    Rep Power
    58
    Oh really then explain why my now working regex won't work unless i escape them?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo