duanqiechui2378 2012-04-20 20:20
浏览 49
已采纳

php数组仍然获得空格和空值

trying to get two word phrases from an array but I keep getting one word phrases with a white space either before or after the word.

$text = preg_replace(array('/\s{2,}/', '/[\t
]/'), ' ', $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 == "
"  || $tey == "\t"  || $tey == ""  || $tey == "
" || $tey == "&nbsp;" || $tey == "  " || $tey == " ") {
        //do nothing
    } else {
        echo $val2."
";
    }
}

and for some reason this just returns values likes whitespace Hello or Test and then a whitespace, but I want it to return hello test

  • 写回答

2条回答 默认 最新

  • douyabu1528 2012-04-20 20:30
    关注

    No idea what the second half of the script is supposed to do but the first part can be reduced to one preg_split() line of code

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

    prints

    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"
    }
    

    edit: maybe you're looking for something like this

    <?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)
    ",
            $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;
    }
    

    which prints

    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)
    

    see http://docs.php.net/class.cachingiterator

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测