欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP 显示关键字前后X个单词

程序员文章站 2022-03-21 10:00:13
...
$search = "want";   //搜寻的关键字$content = "There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do";       //如何搜寻出 "want"   所在段落前后30个字符//单词不能被拆分//内容里出现多个“want” 只搜最前一个


回复讨论(解决方案)

$search = "want";   //搜寻的关键字$content = "There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do";$search = preg_quote($search);preg_match("/\b.{1,30}{$search}.{1,30}\b/", $content, $m);echo $m[0];
someone so much that you just want to pick them from your dreams