PHP:有条件插入

我有这样的查询:


$content = "How technology is helping to change the way people think about the food on their plate and the food impact for them. Technology could have a role to play in raising awareness of the impact our diets have on the planet.";


$exp = explode(" ", $content)


for($j = 0; $j < count($exp); $j++){

    $this->db->query("INSERT INTO news (news_id, news_content) VALUES ('$id', $exp[$j])");

}


但是,我不想插入所有的词,我只需要插入只出现不止一次的词(技术、食品、影响)。有可能这样做吗?有人能帮我吗?


慕斯709654
浏览 136回答 2
2回答

MYYA

我会使用排除停用词列表中的单词来处理文本内容array_filter,然后计算每个单词的出现次数array_count_values,然后array_filter计算出只出现一次的单词。然后您可以将剩余的单词(这将是输出数组的键)写入数据库。例如:$content = "How technology is helping to change the way people think about the food on their plate and the food impact for them. Technology could have a role to play in raising awareness of the impact our diets have on the planet.";$stopwords = array('how', 'is', 'to', 'the', 'way', 'on', 'and', 'for', 'a', 'in', 'of', 'our', 'have');// count all words in $content not in the stopwords list$counts = array_count_values(array_filter(explode(' ', strtolower($content)), function ($w) use ($stopwords) {&nbsp; &nbsp; return !in_array($w, $stopwords);}));// filter out words only seen once$counts = array_filter($counts, function ($v) { return $v > 1; });// write those words to the databaseforeach ($counts as $key => $value) {&nbsp; &nbsp; $this->db->query("INSERT INTO news (news_id, news_content) VALUES ('$id', '$key')");}对于您的示例数据,最终结果$counts将是:Array(&nbsp; &nbsp; [technology] => 2&nbsp; &nbsp; [food] => 2&nbsp; &nbsp; [impact] => 2)

慕标琳琳

我相信这里有很多选择。这是我的解决方案:您可以使用search_array()它。如果在数组中的 in_array 中未找到其他针,则搜索数组返回 false。如果找到另一个词,它会返回密钥。根据您的需要,您可以使用以下这些选项之一。//Option 1//Words that actually appear more than once...&nbsp;$new_arr = array();foreach($exp as $key=>$e) {&nbsp; &nbsp; //Must be this word only (therefore the true-statement&nbsp; &nbsp; $search = array_search($e, $exp, true);&nbsp;&nbsp; &nbsp; if ($search !== false && $search != $key) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $new_arr[] = $e;&nbsp; &nbsp; }}//Option 2////Your question was not totally clear so I add this code as well//Words with asterixes before and after that appear more than once$new_arr = array();foreach($exp as $key=>$e) {&nbsp; &nbsp; //Two asterixes at the beginning of the sting and two at the end&nbsp; &nbsp; //strtolower sets **Technology** and **technology** as a duplicate of word&nbsp; &nbsp; if (substr($e,0,2) == "**" && substr($e,-2,2) == "**") {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $search = array_search(strtolower($e), $exp);&nbsp; &nbsp; &nbsp; &nbsp; if ($search !== false && $search != $key) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_arr[] = $e;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}for($j = 0; $j < count($new_arr); $j++){&nbsp; &nbsp; $this->db->query("INSERT INTO news (news_id, news_content)&nbsp;&nbsp; &nbsp; VALUES ('$id', $new_arr[$j])");}正如有人在评论中提到的那样,您应该通过在 INSERT 语句中输入这种方式来防止 SQL 注入(您应该这样做),但问题主要是关于在字符串中查找重复项以对它们执行某些操作,因此我不会更进一步有了那个评论。结果数组$new_arr如下:(选项 1)array (size=9)&nbsp; 0 => string 'the' (length=3)&nbsp; 1 => string 'the' (length=3)&nbsp; 2 => string '**food**' (length=8)&nbsp; 3 => string 'to' (length=2)&nbsp; 4 => string 'the' (length=3)&nbsp; 5 => string '**impact**' (length=10)&nbsp; 6 => string 'have' (length=4)&nbsp; 7 => string 'on' (length=2)&nbsp; 8 => string 'the' (length=3)Technology和technology之所以不一样,是因为它在其中一个词中是大写的 T。结果数组$new_arr如下:(选项 2)array (size=3)&nbsp; 0 => string '**food**' (length=8)&nbsp; 1 => string '**Technology**' (length=14)&nbsp; 2 => string '**impact**' (length=10)
打开App,查看更多内容
随时随地看视频慕课网APP