Jan Oppeneer
Jan Oppeneer

Reputation: 47

remaining words of string in php

I have a piece of code that shows the first 20 words on my webpage.

$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)) . '...';

This works fine but I want to display the remaing text too without repeating the first 20 words how can I do this?

Upvotes: 1

Views: 160

Answers (2)

Jan Prieser
Jan Prieser

Reputation: 1528

without rewriting anything, just save the content of

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

before overwriting it. it already contains the remaining text

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

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

Personally, I'd do something like this:

$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,"");

Now $result contains two elements: The first elements is the first 20 words of the story, the second is everything after.

Upvotes: 1

Related Questions