DumDumDummy DumDumDummy - 2 years ago 63
PHP Question

How to use regex match if the string contains user-defined command for emoticon

I am creating an application and I'm making command for emoticons.

When you type

it displays icon. I already did that.

But what I want is if the user types
/happy happy
it would display the my emoticon + happy word.

How to do this using regex? Another example:

User inputs:
/happy /happy /happy happy

There should be 3 emoticons and 1 word.

Answer Source

I mean you don't even need a regex for this, but if you wanted to use one, it's as simple as:

$string = preg_replace('@/happy\b@i', ':)', $string);

The \b is a "word boundary" which would prevent "/happyer" from turning into :)er.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download