所以目前我正在使用此代码仅从字符串
中获取第一个句子preg_match('/^([^.!?]*[\.!?]+){0,1}/', $text, $abstract);
你能帮我解决一下如何创建另一个正则表达式来获取剩下的文本或仅在第一句后得到文本吗?
由于
答案 0 :(得分:1)
如果您知道该字符串中有多少句话,这可能会对您有所帮助。
$str = "First Sentence.";
$str .= "Second Sentence. Third Sentence";
$result = explode(".",$str)[1].". ".explode(".",$str)[2];
echo $result;
更新
最终答案>>
$str = "First Sentence.";
$str .= "Second Sentence. Third Sentence";
$extract = strpos($str, ".")+1;
$result = substr($str, $extract, strlen($str));
echo $result;
答案 1 :(得分:1)
这应该使用explode()
:
<?php
$string = 'Sentence one. Sentence two. Sentence three. Sentence four.';
$sentences = explode(".", $string);
echo $sentences[0]; // echos 'Sentence one'
echo $sentences[1]; // echos ' Sentence two'
echo $sentences[2]; // echos ' Sentence three'
echo $sentences[3]; // echos ' Sentence four'
// The following demonstrates how to do what you're asking, but I'm not quite
// sure what your specific use case is so adapt as necessary.
echo $sentences[0]; // echos 'Sentence one'
// to echo the remaining sentences do this
// start at 1 not 0 to skip the first sentence
for ($i = 1; $i < count($sentences); $i++)
{
echo $sentences[$i];
}
请注意,这会处理任何'。'作为一个句子的结尾所以它可能不适用于所有情况,例如,如果你有'I.T.'中期判决。因此,如果您需要此级别的准确性,正则表达式可能是更合适的解决方案。如果您有任何疑问,请告诉我。 :)