Re: [PHP] Re: Substr by words

2005-10-31 Thread Gustavo Narea
Hello, Marcus. No, you are right. Your script is better. I just forgot something I learned about REGEXES: The REGEX engine is eager. Thus, in this case, It's not necessary to use the caret. The REGEX engine will start from the first word It finds. I would use yours ;-). Best regards, Gusta

Re: [PHP] Re: Substr by words

2005-10-31 Thread Marcus Bointon
On 31 Oct 2005, at 03:29, Gustavo Narea wrote: I think It is OK what I said about the caret, but what we need to change is the position of \W*: Your suggestion: /(\b\w+\b\W*){1,$MaxWords}/ My suggestion: /^(\W*\b\w+\b){1,$MaxWords}/ We need the *first* ($MaxWords)th words. I makes no

Re: [PHP] Re: Substr by words

2005-10-30 Thread Gustavo Narea
Hello, Marcus. Marcus Bointon wrote: On 30 Oct 2005, at 15:35, Gustavo Narea wrote: I think that trim($matches[0]) will return the whole string with no change. No, it will return the entire matching pattern, not just the sub- matches. I added the trim to remove any leading space, and there w

Re: [PHP] Re: Substr by words

2005-10-30 Thread Marcus Bointon
On 30 Oct 2005, at 15:35, Gustavo Narea wrote: I think that trim($matches[0]) will return the whole string with no change. No, it will return the entire matching pattern, not just the sub- matches. I added the trim to remove any leading space, and there will nearly always be a trailing sp

Re: [PHP] Re: Substr by words

2005-10-30 Thread Gustavo Narea
Other mistake in my last script. Gustavo Narea wrote: $MyOriginalString = "This is my original string.\nWhat do you think about this script?"; $MaxWords = 6; // How many words are needed? $replacement = preg_match("/^(\W*\b\w+\b){1,$MaxWords}/", '', $MyOriginalString); $result = substr( $MyOr

Re: [PHP] Re: Substr by words

2005-10-30 Thread Gustavo Narea
Hello. Marcus Bointon wrote: On 30 Oct 2005, at 06:22, Gustavo Narea wrote: 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 =

Re: [PHP] Re: Substr by words

2005-10-30 Thread Marcus Bointon
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 th

[PHP] Re: Substr by words

2005-10-29 Thread Gustavo Narea
My script will fail if the amount of words in $MyOriginalString is less than $MaxWords. So, this is the new suggestion: $MyOriginalString = "This is my original string.\nWhat do you think about this script?"; $MaxWords = 50; // How many words are needed? $replacement = ereg_replace ("^([[:spa

[PHP] Re: Substr by words

2005-10-29 Thread Gustavo Narea
Gustavo Narea wrote: 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, And punctuation marks. Sorry, I'm very forgetful tonight! Cheers. -- PHP General Mailing List (http://www.php.net/) To unsubsc

[PHP] Re: Substr by words

2005-10-29 Thread Gustavo Narea
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:

[PHP] Re: Substr by words

2005-10-29 Thread Gustavo Narea
Hello. What do you think about this: $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}", "",$MyOri