我想知道 laravel 中 eloquent 中使用了多少單詞。我不確定 eloquent 是否可以,所以如果不是,我該如何使用 php 來做到這一點?例子 :我從以下查詢中得到結果: $posts = Posts::where('post_body', 'like', '%'.$name.'%')->get(); $result = []; foreach ($posts as $post) { // Logic here to push to result and how much searched word used in result and push to array } return $result;因此,如果我搜索文件名之類的內容12345.jpeg,我想12345.jpeg知道post_body. 檢查有多少,然后將使用計數和結果推送到數組。更多示例:發布正文結果:“這是 12345.jpeg 且<img src="12345.jpeg"/>” => 返回字符串中 12345.jpeg 的 2 次使用情況發布正文結果:“這是 12345.jpeg 以及<img src="12345.jpeg"/>更多 12345.jpeg”=> 返回字符串中 12345.jpeg 的 3 次使用情況post body result : => 返回結果中使用了happy world, happy life, Happy post多少。happy這是我對 1 感到滿意以來的 2 次H。
2 回答

拉丁的傳說
TA貢獻1789條經驗 獲得超8個贊
可以使用PHP的substr_count
函數。
例如:
$s = "happy world, happy life, Happy post"; echo substr_count($s, "happy"); //yields 2.
來自文檔:substr_count()返回 Needle 子字符串在 haystack 字符串中出現的次數。請注意,針區分大小寫。
供參考的文檔鏈接: https ://www.php.net/manual/en/function.substr-count.php

牧羊人nacy
TA貢獻1862條經驗 獲得超7個贊
這就是您所需要的。
$posts = Posts::where('post_body', 'like', '%'.$name.'%')->get();
$posts->transform(function ($post) use ($name) {
$post = (object) $post;
$post->occurennces = substr_count($post->post_body, $name);
return $post;
});
return $posts;
- 2 回答
- 0 關注
- 129 瀏覽
添加回答
舉報
0/150
提交
取消