cvs: pear /PHP_Compat/Compat/Function str_word_count.php
| From: | Aidan Lister | Date: | Sat, 26 Jun 2004 07:21:26 +0000 |
| Subject: | cvs: pear /PHP_Compat/Compat/Function str_word_count.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-21140@lists.php.net to get a copy of this message | ||
aidan Sat Jun 26 03:21:26 2004 EDT
Modified files:
/pear/PHP_Compat/Compat/Function str_word_count.php
Log:
Reduced code bloat
http://cvs.php.net/diff.php/pear/PHP_Compat/Compat/Function/str_word_count.php?r1=1.1&r2=1.2&ty=u
Index: pear/PHP_Compat/Compat/Function/str_word_count.php
diff -u pear/PHP_Compat/Compat/Function/str_word_count.php:1.1
pear/PHP_Compat/Compat/Function/str_word_count.php:1.2
--- pear/PHP_Compat/Compat/Function/str_word_count.php:1.1 Sat Jun 26 03:14:20 2004
+++ pear/PHP_Compat/Compat/Function/str_word_count.php Sat Jun 26 03:21:26 2004
@@ -16,7 +16,7 @@
// | Authors: Aidan Lister <aidan@php.net> |
// +----------------------------------------------------------------------+
//
-// $Id: str_word_count.php,v 1.1 2004/06/26 07:14:20 aidan Exp $
+// $Id: str_word_count.php,v 1.2 2004/06/26 07:21:26 aidan Exp $
//
@@ -27,7 +27,7 @@
* @package PHP_Compat
* @link http://php.net/str_word_count
* @author Aidan Lister <aidan@php.net>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
* @since PHP 4.3.0
* @require PHP 4.0.1 (trigger_error)
*/
@@ -39,27 +39,23 @@
trigger_error("str_word_count() The specified format parameter,
'$format' is invalid", E_USER_WARNING);
return false;
}
-
+
+ $word_string = preg_replace('/[0-9]+/', '', $string);
+ $word_array = preg_split('/[^A-Za-z0-9_\']+/', $wordstr, -1,
PREG_SPLIT_NO_EMPTY);
+
switch ($format):
case null:
- $wordstr = preg_replace('/[0-9]+/', '', $string);
- $count = count(preg_split('/[^A-Za-z0-9_\']+/', $wordstr, -1,
PREG_SPLIT_NO_EMPTY));
-
- return $count;
+ return count($word_array);
break;
case 1:
- $wordstr = preg_replace('/[0-9]+/', '', $string);
- return preg_split('/[^A-Za-z0-9_\']+/', $wordstr, -1, PREG_SPLIT_NO_EMPTY);
+ return $word_array;
break;
case 2:
- $wordstr = preg_replace('/[0-9]+/', '', $string);
- $matches = preg_split('/[^A-Za-z0-9_\']+/', $wordstr, -1, PREG_SPLIT_NO_EMPTY);
-
$lastmatch = 0;
- foreach ($matches as $match) {
- $array[$lastmatch = strpos($string, $match, $lastmatch)] = $match;
+ foreach ($word_array as $word) {
+ $array[$lastmatch = strpos($string, $word, $lastmatch)] = $word;
}
return $array;
break;