cvs: pear /PHP_Compat/Compat/Function str_word_count.php /PHP_Compat/tests/function str_word_count.phpt

From: Date: Sat, 26 Jun 2004 07:14:21 +0000
Subject: cvs: pear /PHP_Compat/Compat/Function str_word_count.php /PHP_Compat/tests/function str_word_count.phpt
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-21139@lists.php.net to get a copy of this message
aidan Sat Jun 26 03:14:21 2004 EDT Added files: /pear/PHP_Compat/Compat/Function str_word_count.php /pear/PHP_Compat/tests/function str_word_count.phpt Log: Initial Commit http://cvs.php.net/co.php/pear/PHP_Compat/Compat/Function/str_word_count.php?r=1.1&p=1 Index: pear/PHP_Compat/Compat/Function/str_word_count.php +++ pear/PHP_Compat/Compat/Function/str_word_count.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Aidan Lister <aidan@php.net> | // +----------------------------------------------------------------------+ // // $Id: str_word_count.php,v 1.1 2004/06/26 07:14:20 aidan Exp $ // /** * Replace str_word_count() * * @category PHP * @package PHP_Compat * @link http://php.net/str_word_count * @author Aidan Lister <aidan@php.net> * @version $Revision: 1.1 $ * @since PHP 4.3.0 * @require PHP 4.0.1 (trigger_error) */ if (!function_exists('str_word_count')) { function str_word_count ($string, $format = null) { if ($format != 1 && $format != 2 && $format !== null) { trigger_error("str_word_count() The specified format parameter, '$format' is invalid", E_USER_WARNING); return false; } 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; break; case 1: $wordstr = preg_replace('/[0-9]+/', '', $string); return preg_split('/[^A-Za-z0-9_\']+/', $wordstr, -1, PREG_SPLIT_NO_EMPTY); 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; } return $array; break; endswitch; } } ?> http://cvs.php.net/co.php/pear/PHP_Compat/tests/function/str_word_count.phpt?r=1.1&p=1 Index: pear/PHP_Compat/tests/function/str_word_count.phpt +++ pear/PHP_Compat/tests/function/str_word_count.phpt --TEST-- Function -- str_word_count --SKIPIF-- <?php if (function_exists('str_word_count')) { echo 'skip'; } ?> --FILE-- <?php require_once 'PHP/Compat.php'; PHP_Compat::loadFunction('str_word_count'); $str = "Hello friend, you're \r\nsdf\tlooking 3865\t9879 good to\"day, yes \"sir\" you am!"; var_dump(str_word_count($str)); print_r(str_word_count($str, 1)); print_r(str_word_count($str, 2)); ?> --EXPECT-- int(12) Array ( [0] => Hello [1] => friend [2] => you're [3] => sdf [4] => looking [5] => good [6] => to [7] => day [8] => yes [9] => sir [10] => you [11] => am ) Array ( [0] => Hello [6] => friend [14] => you're [23] => sdf [27] => looking [48] => good [53] => to [56] => day [61] => yes [66] => sir [71] => you [75] => am )

« previous php.pear.cvs (#21139) next »