Re: Substr by words

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

 



If forgot to say that It counts ($MaxWords) words, It doesn't matter if they're separated by simple spaces, line feeds (Unix, dos or mac), tabs, among others.

On the other hand, you won't have any problem if you use non-English characters.

Best regards,

Gustavo Narea.

Gustavo Narea wrote:
Hello.

What do you think about this:

<?php

$MyOriginalString = "This is my original string.\nWhat do you think about this script?";
$MaxWords = 6; // How many words are needed?

echo substr( $MyOriginalString, 0, -strlen(ereg_replace ("^([[:space:]]*[^[:space:][:cntrl:]]+){1,$MaxWords}", "",$MyOriginalString)));
?>

Only 3 lines.

You have to change $MaxWords to 50 if that's what you need.

Best regards,

Gustavo Narea.

Danny wrote:

Hi,
I need to extract 50 words more or less from a description field. How can i
do that?. Substr, cuts the words. Is there any other way to that, without
using and array? I mean and implemented function in PHP 4.x
 I´ve been googling around, but wordwrap, and substr is driving me mad...
 Thanks in advance
Best Regards

--
dpc


--
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