gpt4 book ai didi

javascript - 检查从 preg_replace() 返回的值是否存在于数组中

转载 作者:行者123 更新时间:2023-11-29 19:16:17 25 4
gpt4 key购买 nike

我有一个消息字符串例如:

$data="Hey my dear :laugh: how are you :mmmmm: :smile: and non existing smile :go: ";

我有微笑数组(用于检查)

$smileys=array("laugh","mmmm","smile");

昨天 friend 帮我把:smile:类型转换成enter image description here喜欢下来

$data=preg_replace("/:([a-zA-Z]+):/","<img src='images/smileys/$1.png' class='smile'>",$data);

我如何检查数组中是否存在这个微笑?

示例:http://masters.az
登录:测试
通过:测试
代码示例:http://masters.az/message-3

最佳答案

我会用 内爆术语,然后在替换中使用找到的术语。

$data = "Hey my dear :laugh: how are you :mmmmm: :smile: and non existing smile :go: ";
$smileys = array("laugh","mmmm","smile");
echo preg_replace('/:(' . implode('|', $smileys) . '):/', '<img src="images/smileys/$1.png" class="$1">', $data);

PHP 演示:https://eval.in/511095

Regex101 演示:https://regex101.com/r/hI1aX0/1

这是一个 JS 方法:

var test = ["laugh","mmmm","smile"];
var regex = new RegExp(':(' + test.join('|') + '):', 'g');
var string = "Hey my dear :laugh: how are you :mmmmm: :smile: and non existing smile :go: ";
var string = string.replace(regex, '<img src="images/smileys/$1.png" class="$1">');
console.log(string);
console.log(regex);

演示:https://jsfiddle.net/8fj00bg5/

关于javascript - 检查从 preg_replace() 返回的值是否存在于数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35129597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com