Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Find words starting with XXX...

Last post 01-25-2008, 9:38 PM by ddrudik. 4 replies.
Sort Posts: Previous Next
  •  01-25-2008, 2:22 PM 38943

    Find words starting with XXX...

    I hope this is an easy question. I'm still extremely new to regex.

    I'm building a  php/mysql chatroom. I would like to be able to have emoticons. I also want to be able to add emoticons easily. So here's what I would like to happen:

    User types: hey everyone vissmile hows everything going? visfronw

    I need an expression that finds all words starting with vis, and returns the entire word. Then I can take that word, compare it to a database. If the  word is in the database, I can replace word with the appropriate img tag. If the word isn't found (i.e. visfronw because frown is mispelled) it doesn't to any replacing. It just leaves visfronw in the text line.

    And I need to be able to make sure replacing vissmile won't affect vissmiles.

    I have looked on the internet, but perhaps I'm not looking for the right thing because I haven't found an answer.

    -YungBlood
     

  •  01-25-2008, 3:37 PM 38944 in reply to 38943

    Re: Find words starting with XXX...

    You must have been looking for the wrong thing

    vis\S+

    will match the values in your sample text 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  01-25-2008, 6:03 PM 38948 in reply to 38943

    Re: Find words starting with XXX...

    This seems like a perfect situation for preg_match_callback.  If you care to share your emoticon word list I can provide an example of its usage in this case.


  •  01-25-2008, 8:29 PM 38951 in reply to 38948

    Re: Find words starting with XXX...

    I tried the vis\S+ in regexlib.com and it almost worked perfectly.

    I entered some data, and put that in there, and it picked up the entire words. But it also picked up revistest (when vis is not the first 3 letters). True, on revistest, the result was vistest. But I would like it to only see words that start with vis.

    I can't provide list of emoticons, because it will be in a database. I want the admins of my site to be able to add in pics easily. The database will be 2 columns the keyword & the path to the img file.

    -Kevin 

  •  01-25-2008, 9:38 PM 38952 in reply to 38951

    Re: Find words starting with XXX...

    In general, this is how it would work:

    <?php
    function get_emoticon($matches) {
      //insert your code to query db for $matches[0] match
      //if found, then
      return $imgtagusingpathtoimgfile;
      //if not found, then
      return $matches[0];
    }
    $pattern = '/\bvis\S+/';
    $output = preg_replace_callback($pattern, 'get_emoticon', $input);
    echo $output;
    ?>


View as RSS news feed in XML