Re: Show only few words that var contains

From: Date: Wed, 26 Dec 2001 19:25:15 +0000
Subject: Re: Show only few words that var contains
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-78786@lists.php.net to get a copy of this message
Daniel, Here is a rather inelegant way to do this. I am sure there is a better way and I would love for someone to point it out to us. This function simply pulls out the first N words using the space as a delimiter. function firstN($string, $N) { // remove all double and more spaces $string = ereg_replace("[[:space:]][[:space:]]+", " ",$string); // explode the string on spaces, slice off the amount of words you want and then implode it back to a single string $return = implode(" ", array_slice (explode(" ",$string, $N + 1), 0, $N)); // return it return $return; } //end firstN

« previous php.general (#78786) next »