所以目前我正在使用此代码仅从字符串中获取第一句话
preg_match('/^([^.!?]*[\.!?]+){0,1}/', $text, $abstract);
您能帮助我如何创建另一个正则表达式以获取剩余文本或仅在第一句之后获取文本吗?
谢谢
这应该给您使用的一般思路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];
}
请注意,这将处理任何“。”。作为句子的结尾,因此可能不适用于所有情况,例如,如果您的句子中间有“ IT”字样。因此,如果您需要此级别的准确性,则正则表达式可能是更合适的解决方案。如果您有任何疑问,请告诉我。:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句