|
Posted by Marcus Bointon on 10/30/05 14:56
On 30 Oct 2005, at 06:22, Gustavo Narea wrote:
> $replacement = ereg_replace ("^([[:space:]]*[^[:space:][:cntrl:]]+)
> {1,$MaxWords}", "",$MyOriginalString);
>
> echo substr( $MyOriginalString, 0, ($replacement) ? -strlen
> ($replacement) : strlen($MyOriginalString));
You could get the regex to do the search and the extraction in one go:
$MyOriginalString = "This is my original string.\nWhat do you think
about this script?";
$MaxWords = 6; // How many words are needed?
$matches = array();
if (preg_match("/(\b\w+\b\W*){1,$MaxWords}/", $MyOriginalString,
$matches)) {
$result = trim($matches[0]);
echo $result;
}
Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk
Navigation:
[Reply to this message]
|