从第一个句子之后的字符串中获取所有文本
Get all of the text from a string after the first one sentence
所以目前我正在使用此代码仅从字符串中获取第一句话
preg_match('/^([^.!?]*[\.!?]+){0,1}/', $text, $abstract);
你能帮我看看如何创建另一个正则表达式来获取剩余的文本或只获取第一句话之后的文本吗?
谢谢
如果您知道该字符串中究竟有多少个句子,这可能会对您有所帮助。
$str = "First Sentence.";
$str .= "Second Sentence. Third Sentence";
$result = explode(".",$str)[1].". ".explode(".",$str)[2];
echo $result;
UPDATE
最终答案>>
$str = "First Sentence.";
$str .= "Second Sentence. Third Sentence";
$extract = strpos($str, ".")+1;
$result = substr($str, $extract, strlen($str));
echo $result;
使用 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.' 个中间句子。因此,如果您需要这种级别的准确性,正则表达式可能是更合适的解决方案。如果您有任何问题,请告诉我。 :)
所以目前我正在使用此代码仅从字符串中获取第一句话
preg_match('/^([^.!?]*[\.!?]+){0,1}/', $text, $abstract);
你能帮我看看如何创建另一个正则表达式来获取剩余的文本或只获取第一句话之后的文本吗?
谢谢
如果您知道该字符串中究竟有多少个句子,这可能会对您有所帮助。
$str = "First Sentence.";
$str .= "Second Sentence. Third Sentence";
$result = explode(".",$str)[1].". ".explode(".",$str)[2];
echo $result;
UPDATE
最终答案>>
$str = "First Sentence.";
$str .= "Second Sentence. Third Sentence";
$extract = strpos($str, ".")+1;
$result = substr($str, $extract, strlen($str));
echo $result;
使用 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.' 个中间句子。因此,如果您需要这种级别的准确性,正则表达式可能是更合适的解决方案。如果您有任何问题,请告诉我。 :)