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 = array();
if (preg_match("/(\b\w+\b\W*){1,$MaxWords}/", $MyOriginalString,
$matches)) {
$result = trim($matches[0]);
echo $result;
}
I have not used preg_* functions yet, so I may be wrong:
I think that trim($matches[0]) will return the whole string with no
change. On the other hand, I think we have to place a caret after the
first slash.
What about this:
<?php
$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)) {
unset($matches[0]);
$result = implode(" ",$matches);
echo $result;
}
?>
By the way, if you're able to use preg_* functions, I suggest you to use
this script instead of the former I suggested. What's the difference?
Let's suppose we have a string with typos such as "Mandriva , Red Hat ,
Debian" (the right one is "Mandriva, Red Hat, Debian", without spaces
before commas). The former script will find 6 words (because of the
spaces before commas), while the latter will find 4 words (Mandriva Red
Hat Debian). In this case, the former was wrong and the latter right.
However, the former doesn't not remove punctuation marks nor spaces
(tabs, fine feeds, among others); the latter will remove any character
which is a non-word character. If you need words + punctuation marks +
spaces up to the ($MaxWords)th word, this is my suggestion:
<?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;
?>
Best regards,
Gustavo Narea.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php