当前位置:Gxlcms > PHP教程 > PHP显示关键字前后X个单词

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

时间:2021-07-01 10:21:17 帮助过:34人阅读

$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

人气教程排行