我正在尝试从数据库文本条目中检索第一句和最后一句。
我的代码在这个例子中运行良好:
$text = "He was doing ok so far, but this one had stumped him. He was a bit lost..."
功能:
function first_sentence($content) {
$pos = strpos($content, '.');
if($pos === false) {
return $content;
}
else {
return substr($content, 0, $pos+1);
}
} // end function
// Get the last sentence
function last_sentence($content) {
$content = array_pop(array_filter(explode('.', $content), 'trim'));
return $content;
} // end function
最后一个句子功能考虑到句子末尾的任何尾随...,但两者都无法处理以下情况:
$text = "Dr. Know-all was a coding master, he knew everything and was reputed the world over. But the Dr. was in trouble..."
结果:第一句话:Dr. 最后一句话:有麻烦了
我需要修改函数以考虑“博士”之类的内容。和其他这样的缩写,如果可能的话,所以最后一个文本变量会出来:
第一句话:Dr. Know-all 是个编程高手,他什么都懂,名扬四海 最后一句话:但是 Dr. 有麻烦了
可以做到吗?非常感谢任何帮助!