2012-04-20 4 views
0

배열에서 두 단어 구를 얻으 려하지만 단어 앞이나 뒤에 공백이있는 한 단어 구를 계속 가져옵니다. PHP 배열은 여전히 ​​공백과 null을 얻고 있습니다

$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

스크립트의 두 번째 절반은 어떻게해야 뭔지 생각이 하지만, 첫번째 부분은 하나의 코드 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; 
} 

같은 뭔가를 찾고있어 얼마나 많은 공백, 탭 또는 라인 - 거기에 있습니다

$text = trim(preg_replace('/[\s\t\r\n]+/', ' ', $text)); 
$firstTwoWords = substr($text, 0, strpos($text, ' ', strpos($text, ' ') + 1)); 
관련 문제