我有一个包含 100 多个名称的变量,如下所示:
$var = "Josh" . ";" . "Chloe" . ";" . "Marie" . ";" . "John" . ";"...
如果上面的名字出现在这个文本中,我想做一些事情的一些文本:
$text1 = "this is an apple"; // do nothing, no name here.
$text2 = "marie is beautiful"; marie is in text2, so I want to call a function.
如果 $var 中的一个名字在 $text2 中,我想调用一个函数来执行如下操作:
if name found{
function insert();
}
关于如何检查 $var 中的某个名称是否出现在 $text 中的一些想法?
谢谢 friend 们!
最佳答案
将名称存储在数组中,而不是字符串中:
$names = array('Josh', 'Chloe');
$text1 = 'marie is beautiful';
foreach ($names as $name) {
if (stristr($text1, $name) !== false) {
// name found. Call function here
}
}
如果不能将名称存储在数组中,则分解该字符串:
$names = explode(';', $var);
关于php - 超过 100 个名称的 var,检查文本中是否存在任何名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35247640/