检查从 preg_replace() 返回的值是否存在于数组中
Check value returning from preg_replace() exists or no in array
我有一个消息字符串
例如:
$data="Hey my dear :laugh: how are you :mmmmm: :smile: and non existing smile :go: ";
我有微笑数组(用于检查)
$smileys=array("laugh","mmmm","smile");
昨天朋友帮我把:smile:
类型转换成喜欢下
$data=preg_replace("/:([a-zA-Z]+):/","<img src='images/smileys/.png' class='smile'>",$data);
我如何检查数组中是否存在这个微笑?
示例:http://masters.az
login:test
pass:test
代码示例:http://masters.az/message-3
您可以使用 in_array() 函数。解释是here
那你应该试试这个:-
$data="Hey my dear :laugh: how are you :mmmmm: :smile: and non existing smile :go: ";
$smileys=array("laugh","mmmm","smile");
foreach ($smileys as $sm)
{
$data1=preg_replace("/:([a-zA-Z]+):/",$sm,"<img src='images/smileys/.png' class='smile'>");
if(stristr($data,$sm))
{
echo $sm." matched"."<br>";
}
}
参考是Here
您可以使用 preg_replace_callback:
$data = "Hey my dear :laugh: how are you :mmmmm: :smile: and non existing smile :go: ";
$smileys = array("laugh","mmmm","smile");
$data = preg_replace_callback("/:([a-zA-Z]+):/",
function ($m) use($smileys) {
if (in_array($m[1], $smileys) )
return "<img src='images/smileys/$m[1].png' class='smile'>";
},
$data);
echo $data,"\n";
输出:
Hey my dear <img src='images/smileys/laugh.png' class='smile'> how are you <img src='images/smileys/smile.png' class='smile'> and non existing smile
我会用 or
分解术语,然后在替换中使用找到的术语。
$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/.png" class="">', $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/.png" class="">');
console.log(string);
console.log(regex);
我有一个消息字符串 例如:
$data="Hey my dear :laugh: how are you :mmmmm: :smile: and non existing smile :go: ";
我有微笑数组(用于检查)
$smileys=array("laugh","mmmm","smile");
昨天朋友帮我把:smile:
类型转换成
$data=preg_replace("/:([a-zA-Z]+):/","<img src='images/smileys/.png' class='smile'>",$data);
我如何检查数组中是否存在这个微笑?
示例:http://masters.az
login:test
pass:test
代码示例:http://masters.az/message-3
您可以使用 in_array() 函数。解释是here
那你应该试试这个:-
$data="Hey my dear :laugh: how are you :mmmmm: :smile: and non existing smile :go: ";
$smileys=array("laugh","mmmm","smile");
foreach ($smileys as $sm)
{
$data1=preg_replace("/:([a-zA-Z]+):/",$sm,"<img src='images/smileys/.png' class='smile'>");
if(stristr($data,$sm))
{
echo $sm." matched"."<br>";
}
}
参考是Here
您可以使用 preg_replace_callback:
$data = "Hey my dear :laugh: how are you :mmmmm: :smile: and non existing smile :go: ";
$smileys = array("laugh","mmmm","smile");
$data = preg_replace_callback("/:([a-zA-Z]+):/",
function ($m) use($smileys) {
if (in_array($m[1], $smileys) )
return "<img src='images/smileys/$m[1].png' class='smile'>";
},
$data);
echo $data,"\n";
输出:
Hey my dear <img src='images/smileys/laugh.png' class='smile'> how are you <img src='images/smileys/smile.png' class='smile'> and non existing smile
我会用 or
分解术语,然后在替换中使用找到的术语。
$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/.png" class="">', $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/.png" class="">');
console.log(string);
console.log(regex);