我试了又试,但我很难过。假设我有以下场景:
$string = "Jenny's garden is one of the best in town, it has lush greens and colorful flowers. With what happened to her recently, she could use a new sprinkler system so that she does not have to over exert herself. Perhaps Joel can sell that extra bike to raise money or perhaps put up a garage sale.";
$keyword = "recently";
$length = 136;
// when keyword keyword is empty
$result = "Jenny's garden is one of the best in town, it has lush greens and colorful flowers. With what happened to her recently, she could use a (snip)";
// when keyword is NOT empty
$result = "(snip)it has lush greens and colorful flowers. With what happened to her recently, she could use a new sprinkler system so that she does not h(snip)";
我想要做的是获取字符串的摘录,如 $result 上所示,可能以关键字的第一次出现为中心(如果存在)。我很困惑如何使用 substr 和 strpos 在 php 中实现这一点。帮助?