Rumah >pembangunan bahagian belakang >tutorial php >PHP 显示关键字前后X个单词

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

WBOY
WBOYasal
2016-06-23 13:05:031036semak imbas

$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

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn