使用PHP提取前2个句子

时间:2013-04-10 18:39:46

标签: strpos

我想出了这段代码,它从文本中提取第一句话。但是我需要提取前两个句子,我无法弄清楚该怎么做。有什么想法吗?

$input = get_field('fl_description');

$pos = strpos($input, '.' , 1);
$output = substr($input, 0, $pos+1);

echo $output;

2 个答案:

答案 0 :(得分:0)

尝试使用带有分隔符“。”的爆炸方法。将限制设置为2,将创建包含2个元素的数组,前两个句子。

  

$ arrayOfSentences = explode('。',$ input,2);

     

echo $ arrayOfSentences [0] +“”+ $ arrayOfSentences [1];

有关爆炸方法的更多信息:http://php.net/manual/pl/function.explode.php

答案 1 :(得分:0)

$input = get_field('fl_description');
$pos = strpos($input, '.' , 1); 
$pos = strpos($input, '.' , $pos+1); 
$output = substr($input, 0, $pos+1);
echo $output; '