如果字符串包含用户定义的表情符号命令,如何使用正则表达式匹配


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

我正在创建一个应用程序,我正在为表情符号制作命令。

当您键入/happy时,它会显示图标。我已经这样做了。

但我想要的是,如果用户键入/happy happy它将显示我的表情符号+快乐词。

如何使用正则表达式执行此操作?再比如:

用户输入:/happy /happy /happy happy

应该有 3 个表情符号和 1 个单词。

您需要

在正则表达式中包含/,以便匹配具有该的单词,如下所示:

$output = preg_replace('~/happy~', 'EMOTICON', $input);

例如:

$input = "I'm so /happy now!";
$output = preg_replace('~/happy~', "☺", $input);
echo $output;
// output: I'm so ☺ now!

渲染输出:我现在就是这样☺!

我的意思是你甚至不需要正则表达式,但如果你想使用一个,它就像

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

'b是一个"词边界",可以防止"/happyer"变成:)er