#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2007
    Location
    Tucson, AZ
    Posts
    592
    Rep Power
    79

    Simple regex for smilies not working?


    Hey there! I'm having an annoying problem, and I can't figure out what's wrong!

    PHP Code:
    $smileyList = array(
        
    "/\bB\)\b/" => "IMAGE CODE REMOVED",
        
    "/\b\:\'\(\b/" => "IMAGE CODE REMOVED",
        
    "/\b\=\'\(\b/" => "IMAGE CODE REMOVED",
        
    "/\booh\b/" => "IMAGE CODE REMOVED",
        
    "/\boooh\b/" => "IMAGE CODE REMOVED",
        
    "/\booooh\b/" => "IMAGE CODE REMOVED",
        
    "/\boooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\booooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\boooooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\booooooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\boooooooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\booooooooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\boooooooooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\booooooooooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\boooooooooooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\booooooooooooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\boooooooooooooooh\b/" => "IMAGE CODE REMOVED",
        
    "/\b\:DD\b/" => "IMAGE CODE REMOVED>",
        
    "/\b\=DD\b/" => "IMAGE CODE REMOVED",
        
    "/\b\:D\b/" => "IMAGE CODE REMOVED",
        
    "/\b\=D\b/" => "IMAGE CODE REMOVED",
        
    "/\b\?\?\?\b/" => "IMAGE CODE REMOVED",
        
    "/\b\>\:\(\b/" => "IMAGE CODE REMOVED",
        
    "/\b\>\=\(\b/" => "IMAGE CODE REMOVED",
        
    "/\b\:\(\b/" => "IMAGE CODE REMOVED",
        
    "/\b\=\(\b/" => "IMAGE CODE REMOVED",
        
    "/\b\:\)\b/" => "IMAGE CODE REMOVED,
        "
    /\b\=\)\b/" => "IMAGE CODE REMOVED",
        "
    /\b\:o\b/" => "IMAGE CODE REMOVED",
        "
    /\b\=o\b/" => "IMAGE CODE REMOVED",
        "
    /\bi\'m tired\b/" => "IMAGE CODE REMOVED",
        "/\b\:P\b/" => "IMAGE CODE REMOVED",
        "/\b\=P\b/" => "IMAGE CODE REMOVED");
    $text = preg_replace(array_keys($smileyList), array_values($smileyList), $text); 
    Why won't this work? Thanks!
    Last edited by requinix; July 29th, 2011 at 01:34 PM.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    Syntax errors are easier to spot with the [php] tags - with [highlight=php] it's too colorful.
    Look at your code.

    Also, learn to use quantifiers. For example, + means to match one or more of the previous thing.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2007
    Location
    Tucson, AZ
    Posts
    592
    Rep Power
    79
    Those syntax errors aren't normally there. It's just because Devshed was complaining I had too many images, so I must have missed it as I was haphazardly going through replacing them out.

    Try here instead.
    http://pastebin.com/rbehxmyy

    So... what's wrong with the regex themselves? I have the word boundaries, and I have the delimiters, I have and I've escaped all of the special characters. I can't figure out what I'm missing.

    Thanks!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    It would be helpful if you gave some examples of text you expected to be matched that was not and the particular regex you expected to match it.

    One problem I see is \b is probably not doing what you want.

    A word boundary matches between a word char (anything that matches \w, e.g. letters) and a non-word char (anything that doesn't match \w).

    Looking at your regex: /\b\:\(\b/ , consider what characters must be before and after the :( in order for it to match.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2007
    Location
    Tucson, AZ
    Posts
    592
    Rep Power
    79
    I just want it to basically match a standard form post. The reason I'm using regular expressions instead of a simple str_replace is because I only want it to match the whole world.

    Code:
    [color=orange] should not match "=o"
    See where I'm coming from? I also allow BBCode, so I'm trying to allow standard smileys as well without conflict.

    Thanks!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,513
    Rep Power
    1424
    I'd rather recommend to use a real bbcode parser, instead of regular expressions. In case you're inside a bbcode tag (or a code block, to give another example) you could either run your smiley replacement or not, depending on the current context.

    Take a look at Christian Seiler's approach here for some inspiration.

    Regards, Jens

IMN logo majestic logo threadwatch logo seochat tools logo