在PHP中剩下的字符串

时间:2012-07-13 14:19:59

标签: php string words

我有一段代码显示了我网页上的前20个单词。

$arrtext = explode( ' ', stripslashes( str_replace('\n',chr(10),str_replace('\r',chr(13),$row['text']))), $config['length_story'] + 1);

$arrtext[ $config['length_story'] ] = '';

$row['headtext'] = trim(implode( ' ', $arrtext)) . '...';

这样可以正常工作但我想显示剩余文本而不重复前20个单词我该怎么做?

2 个答案:

答案 0 :(得分:1)

就个人而言,我会做这样的事情:

$story = $row['text']; // apply whatever here, `stripslashes`, `sre_replace`...
preg_match("/^((?:\S+\s+){20})(.*)$/",$story,$match);
if( $match)
    $result = Array($match[1],$match[2]);
else
    $result = Array($story,"");

现在$result包含两个元素:第一个元素是故事的前20个单词,第二个元素是后面的所有单词。

答案 1 :(得分:1)

无需重写任何内容,只需保存

的内容即可
$arrtext[ $config['length_story'] ] = '';

在覆盖之前。它已包含剩余的文本

$remaining = $arrtext[ $config['length_story'] ];
$arrtext[ $config['length_story'] ] = '';