Re: Re: Substr by words

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxx | http://www.synchromedia.co.uk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux