我已经找到了类似的线程:
$sentence = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $string);
但这似乎在我的函数中不起作用:
<?function first_sentence($content) {
$content = html_entity_decode(strip_tags($content));
$content = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $content);
return $content;
}?>
当句子作为段落的结尾结束时,似乎没有考虑第一句。有任何想法吗?
/**
* Get the first sentence of a string.
*
* If no ending punctuation is found then $text will
* be returned as the sentence. If $strict is set
* to TRUE then FALSE will be returned instead.
*
* @param string $text Text
* @param boolean $strict Sentences *must* end with one of the $end characters
* @param string $end Ending punctuation
* @return string|bool Sentence or FALSE if none was found
*/
function firstSentence($text, $strict = false, $end = '.?!') {
preg_match("/^[^{$end}]+[{$end}]/", $text, $result);
if (empty($result)) {
return ($strict ? false : $text);
}
return $result[0];
}
// Returns "This is a sentence."
$one = firstSentence('This is a sentence. And another sentence.');
// Returns "This is a sentence"
$two = firstSentence('This is a sentence');
// Returns FALSE
$three = firstSentence('This is a sentence', true);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)