2012-04-20 3 views
0

配列から2つの単語フレーズを取得しようとしていますが、私は単語の前後に空白を持つ1つの単語フレーズを取得し続けます。 PHP配列はまだ空白とヌルを受け取ります

$text = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $text); 
$textarray = explode(" ", $text); 
array_filter($textarray); 
$textCount = array_count_values($textarray); 
arsort($textCount); 
$twoWords = array(); 
for ($i = 0; $i<=count($textarray); ++$i) { 
    if ($textarray[$i]<>" ") { 
     $twoWords[$i] = $textarray[$i]." ".$textarray[$i + 1]; 
    } else { 
     $twoWords[$i] = "blah"; 
    } 
} 
foreach ($twoWordsCount as $tey => $val2) { 
    if ($tey == null || $tey == "" || $tey == "\r\n" || $tey == "\t" || $tey == "\r" || $tey == "\n" || $tey == "&nbsp;" || $tey == " " || $tey == " ") { 
     //do nothing 
    } else { 
     echo $val2."\n"; 
    } 
} 

、これは単に値を返す何らかの理由

は空白こんにちはかテストして、空白が好き、私はそれがテストにハロー戻りたい

+0

試した 'trim()'? – Blake

+0

あなたはあなたが使用している '$ text'を提供してもらえますか – Julien

+0

あなたの入力データはどのように見えますか? –

答えて

2

スクリプトの後半を行うことになっているものは考えていませんしかし、最初の部分は、コードの1 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" 
} 
に低減することができます

編集:多分あなたは

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

0

を参照してくださいを印刷し、この

<?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; 
} 

ような何かを探しています-breaks are there

$text = trim(preg_replace('/[\s\t\r\n]+/', ' ', $text)); 
$firstTwoWords = substr($text, 0, strpos($text, ' ', strpos($text, ' ') + 1)); 
関連する問題