Re: Re: Substr by words
| From: | Gustavo Narea | Date: | Sun, 30 Oct 2005 16:51:19 +0000 |
| Subject: | Re: Re: Substr by words | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-224846@lists.php.net to get a copy of this message | ||
Other mistake in my last script.
Gustavo Narea wrote:
<?php $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( $MyOriginalString, 0, ($replacement) ? -strlen($replacement) : strlen($MyOriginalString)); echo $result; ?>Instead of preg_match(), I had to type preg_replace(): <?php $MyOriginalString = "This is my original string.\nWhat do you think about this script?"; $MaxWords = 6; // How many words are needed? $replacement = preg_replace("/^(\W*\b\w+\b){1,$MaxWords}/", '', $MyOriginalString); $result = substr( $MyOriginalString, 0, ($replacement) ? -strlen($replacement) : strlen($MyOriginalString)); echo $result; ?> Best regards, Gustavo Narea.