gpt4 book ai didi

php - 从PHP中的变量返回第一句话

转载 作者:可可西里 更新时间:2023-11-01 00:51:20 24 4
gpt4 key购买 nike

我已经在我得到的地方找到了一个类似的线程:

$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);

关于php - 从PHP中的变量返回第一句话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6554375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com