减去文本的第一句并回显它,然后回显剩余的文本

时间:2011-01-04 12:54:59

标签: php

$content = 'this is a small text';
$content = preg_replace('/\s+?(\S+)?$/', '', substr($content, 0, 100)); // i substract the first 100 chars from the text - only whole words
echo $content;

有没有办法减去文本中的第一个句子并回显它?让我们说,直到文本找到“。”还是“?”或者是“!” ?

然后回复文本的其余部分?

谢谢

3 个答案:

答案 0 :(得分:1)

类似(。*?[?。!])的模式将匹配第一句话。

$content = "First sentence. Then. Many. More.";
preg_match('/(.*?[?.!])(.*)/', $content, $match);

$first_sentence = $match[1];
$the_rest = $match[2];

但这会奏效。

 $content = "First sentence? Then. Many. More.";

答案 1 :(得分:1)

你可以改用strtok。

$content = 'This is a small text. With a second Sentance.';
$first = strtok($content, '.?!');
$rest = substr($content, 0, strlen($first));

这对广告代币来说更容易。

答案 2 :(得分:0)

$content = "First sentence. Then. Many. More.";
preg_match('/^([^.]+)\.\s*(.*)$/', $content, $match);

$first_sentence = $match[1];
$the_rest = $match[2];

第一组([^.]+)匹配一个或多个不是点.

的字符

然后它需要一个点后跟零或更多的空格\.\s*

其余部分直到$content结束:(.*)$