请告诉我,PHP如何从下面的文本中获取前五个句子?
西塞罗着名的对他的演说!政治对手Lucius Sergius Catilina。 Погодасегодняхорошая!
偶尔会有第一次针对Catiline的演说被用于类型标本:quo usque tandem abutere,Catilina,patientia nostra? Quam diu etiam furor iste tuus nos eludet?西塞罗写信。或许不是。
答案 0 :(得分:0)
不确定这是否适合您的需求,但在爆炸手册中有一个“multiexplode”功能。
function multiexplode ($delimiters,$string) {
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
$text = "Cicero famously orated against his! Political opponent Lucius Sergius Catilina. Погода сегодня хорошая!
Occasionally the first Oration against Catiline is taken for type specimens: quo usque tandem abutere, Catilina, patientia nostra? Quam diu etiam furor iste tuus nos eludet? Cicero writing letters. Or maybe not.";
$exploded = multiexplode(array(".", "!", "?"),$text);
print_r(array_slice($exploded,0,5));
然后我使用array_slice来获取数组中的前五个项目。
答案 1 :(得分:-1)
很难用非英语字符和句子的模糊定义进行解析,但下面的代码应该进行调整。也许有一些小调整。最初发现Here正如Blue先生在上面评论的那样。我已经测试过几次并且效果很好。
Function stripSentence($Text, $Number) {
$Stripped = preg_replace('/\s+/',' ',strip_tags($Text));
$Sentences = preg_split('/(\.|\?|\!)(\s)/',$Stripped);
If (COUNT($Sentences) <= $Number) {
Return $Stripped;
} Else {
$Stop = 0;
ForEach($Sentences AS $i => $Sentence) {
$Stop += StrLen($Sentence);
If ($i >= $Number - 1) {
Break;
}
}
$Stop += ($Number * 2);
Return Trim(SubStr($Stripped, 0, $Stop));
}
}