不知道是什麼腳本下半年應該做的事但第一部分可以減小到一個代碼使preg_split()線
<?php
foreach(array('hello world', ' hello world', 'hello world ', ' hello world ') as $input) {
$w = preg_split('!\s+!', $input, -1, PREG_SPLIT_NO_EMPTY);
var_dump($w);
}
打印
array(2) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
}
array(2) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
}
array(2) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
}
array(2) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
}
編輯:也許你正在尋找這樣的事情
<?php
$input = getData();
$w = preg_split('![\s[:punct:]]+!', $input, -1, PREG_SPLIT_NO_EMPTY);
$w = array_count_values($w);
arsort($w);
$ci = new CachingIterator(new ArrayIterator($w));
foreach($ci as $next=>$cnt) {
printf("%s(%d) %s(%d)\n",
$next, $cnt,
$ci->getInnerIterator()->key(), $ci->getInnerIterator()->current()
);
}
function getData() {
return <<< eot
Mary had a little lamb,
whose fleece was white as snow.
And everywhere that Mary went,
the lamb was sure to go.
It followed her to school one day
which was against the rules.
It made the children laugh and play,
to see a lamb at school.
And so the teacher turned it out,
but still it lingered near,
And waited patiently about,
till Mary did appear.
"Why does the lamb love Mary so?"
the eager children cry.
"Why, Mary loves the lamb, you know."
the teacher did reply.
eot;
}
它打印
the(8) lamb(5)
lamb(5) Mary(5)
Mary(5) was(3)
was(3) And(3)
And(3) to(3)
to(3) It(2)
It(2) school(2)
school(2) so(2)
so(2) Why(2)
Why(2) did(2)
did(2) it(2)
it(2) teacher(2)
teacher(2) children(2)
children(2) a(2)
a(2) waited(1)
waited(1) patiently(1)
patiently(1) about(1)
about(1) till(1)
[...]
white(1) went(1)
went(1) (0)
看到http://docs.php.net/class.cachingiterator
試過'trim()'? – Blake 2012-04-20 20:23:48
您能否提供您正在使用的'$ text' – Julien 2012-04-20 20:27:52
輸入數據的外觀如何? – 2012-04-20 20:28:03