将数组中的每个项目检查成一个字符串,并将这些项目与一些 html 一起替换
check for each item in an array into a string and replace those items along with some html
我有 mysqli 名称数组
$names=$row['names']; // in the column names , i have "Abu bakkar siddique,Kim hon tae"
和一个字符串
$string='Abu bakkar siddique and Kim hon tae meets the same result';
爆炸后我喜欢
$nameEx=explode(',', $names);
foreach($nameEx as $name){
if (strpos($string, $name)) {
$new[]=str_replace($name,'<a href="#">'.$name.'</a>', $string);
}
}
$results = implode(", ",$new);
echo $results;
出局是:
<a href="#">Abu bakkar siddique</a> and Kim hon tae meets the same result, Abu bakkar siddique and <a href="#">Kim hon tae</a> meets the same result
如何获得
<a href="#">Abu bakkar siddique</a> and <a href="#">Kim hon tae</a> meets the same result
提前致谢...
你可以这样做:
foreach($nameEx as $name){
if (strpos($string, $name) !== false) {
$string=str_replace($name,'<a href="#">'.$name.'</a>', $string);
}
}
echo $string;
但是如果一个名字包含另一个名字,那么使用这种方法要小心......例如,如果一个人的名字是 Abu bakkar
而另一个是 Abu bakkar siddique
试试这个:
foreach($nameEx as $name){
if (strpos($string, $name) !== false) {
$string=str_replace($name,'<a href="#">'.$name.'</a>', $string);
}
}
echo $string;
您将每个名称替换一次。
您还需要检查 strpos return 是 0(第一次出现的位置)还是 false(没有出现)。在您的示例中,名字不会被替换,因为 strpos 会 return 0 并且 if 语句会通过此迭代。
更新:
它不是很漂亮,但是很管用。首先,您获取名称并按长度对其进行排序。然后,将名称替换为 "markers"。最后用您的实际字符串替换标记。
$names = "Abu bakkar,Abu bakkar siddique,Kim hon tae";
$string = "Abu bakkar siddique, Abu bakkar and Kim hon tae meets the same result. Abu bakkar is name for test";
$nameEx = explode(',', $names);
usort($nameEx, function($a, $b) {
if (strlen($a) < strlen($b)) { return 1; } elseif (strlen($a) == strlen($b)) { return 0; } else { return -1; }
});
foreach($nameEx as $key => $name) {
$string = str_replace($name, "#".$key."#", $string);
}
foreach($nameEx as $key => $name) {
$string = str_replace("#".$key."#", "<a href='#'>".$name.'</a>', $string);
}
echo $string;
我有 mysqli 名称数组
$names=$row['names']; // in the column names , i have "Abu bakkar siddique,Kim hon tae"
和一个字符串
$string='Abu bakkar siddique and Kim hon tae meets the same result';
爆炸后我喜欢
$nameEx=explode(',', $names);
foreach($nameEx as $name){
if (strpos($string, $name)) {
$new[]=str_replace($name,'<a href="#">'.$name.'</a>', $string);
}
}
$results = implode(", ",$new);
echo $results;
出局是:
<a href="#">Abu bakkar siddique</a> and Kim hon tae meets the same result, Abu bakkar siddique and <a href="#">Kim hon tae</a> meets the same result
如何获得
<a href="#">Abu bakkar siddique</a> and <a href="#">Kim hon tae</a> meets the same result
提前致谢...
你可以这样做:
foreach($nameEx as $name){
if (strpos($string, $name) !== false) {
$string=str_replace($name,'<a href="#">'.$name.'</a>', $string);
}
}
echo $string;
但是如果一个名字包含另一个名字,那么使用这种方法要小心......例如,如果一个人的名字是 Abu bakkar
而另一个是 Abu bakkar siddique
试试这个:
foreach($nameEx as $name){
if (strpos($string, $name) !== false) {
$string=str_replace($name,'<a href="#">'.$name.'</a>', $string);
}
}
echo $string;
您将每个名称替换一次。
您还需要检查 strpos return 是 0(第一次出现的位置)还是 false(没有出现)。在您的示例中,名字不会被替换,因为 strpos 会 return 0 并且 if 语句会通过此迭代。
更新:
它不是很漂亮,但是很管用。首先,您获取名称并按长度对其进行排序。然后,将名称替换为 "markers"。最后用您的实际字符串替换标记。
$names = "Abu bakkar,Abu bakkar siddique,Kim hon tae";
$string = "Abu bakkar siddique, Abu bakkar and Kim hon tae meets the same result. Abu bakkar is name for test";
$nameEx = explode(',', $names);
usort($nameEx, function($a, $b) {
if (strlen($a) < strlen($b)) { return 1; } elseif (strlen($a) == strlen($b)) { return 0; } else { return -1; }
});
foreach($nameEx as $key => $name) {
$string = str_replace($name, "#".$key."#", $string);
}
foreach($nameEx as $key => $name) {
$string = str_replace("#".$key."#", "<a href='#'>".$name.'</a>', $string);
}
echo $string;