抓取给定关键字前后的 x 个单词?
如何在 PHP 字符串中抓取给定关键字前后的 [x] 个单词?我正在尝试将针对关键字定制的 mysql 查询的结果标记为片段。
How can I go about grabbing [x] number of words before and after a given keyword in a string in PHP? I am trying to tokenize results from a mysql query tailored to the keyword as a snippet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但如果这个词多次出现,你希望发生什么?或者如果该单词没有出现在字符串中?
编辑
getWords2 的扩展版本,最多返回关键字出现的设定次数
But what do you want to happen if the word appears multiple times? Or if the word doesn't appear in the string?
EDIT
Extended version of getWords2 to return up to a set number of occurrences of the keyword