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

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

WBOY
Lepaskan: 2016-06-23 13:05:03
asal
1035 orang telah melayarinya

$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” 只搜最前一个
Salin selepas log masuk


回复讨论(解决方案)

$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];
Salin selepas log masuk
someone so much that you just want to pick them from your dreams
Salin selepas log masuk

sumber:php.cn
Kenyataan Laman Web ini
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
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan