ホームページ >バックエンド開発 >PHPチュートリアル >PHP はキーワードの前後に X 個の単語を表示します

PHP はキーワードの前後に X 個の単語を表示します

WBOY
WBOYオリジナル
2016-06-23 13:05:031037ブラウズ

$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

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。