gpt4 book ai didi

php - 仅显示数组中包含特定单词的值

转载 作者:行者123 更新时间:2023-12-02 05:49:00 24 4
gpt4 key购买 nike

现在我正在为我的 json 解码结果开发一个搜索功能

我得到的代码是这样的:

<?php
foreach($soeg_decoded as $key => $val){
$value = $val["Value"];
$seotitle = $val["SEOTitle"];
$text = $val["Text"];

echo '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
}
?>

我希望它只显示包含特定单词的结果,该单词存储在名为 $searchText 的变量中。

有没有函数或命令,可以把所有的结果分开,只显示包含$searchText中存储的单词的结果。

像这样:

if($value OR $seotitle OR $text contains $searchText){
echo '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
}

提前致谢。

最佳答案

您可以使用substr_count()

<?php
foreach($soeg_decoded as $key => $val){
$value = $val["Value"];
$seotitle = $val["SEOTitle"];
$text = $val["Text"];

if(substr_count($value, $searchText) || substr_count($seotitle, $searchText) || substr_count($text, $searchText)){
echo '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
}

}
?>

阅读更多:

http://php.net/manual/en/function.substr-count.php

关于php - 仅显示数组中包含特定单词的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30833843/

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