【问题标题】:PHP explode line break and strip blank lines and white spacesPHP爆炸换行符并去除空行和空格
【发布时间】:2018-06-14 01:47:55
【问题描述】:

我有一个名为$output 的字符串,它会返回这样的列表

    Object One


    Object Two


    Object Three

    Object Four
    Object Five

    Object Six

我想用这样的explode把它变成一个数组:

explode("\n", $output);

但是,开头有很多空白行和随机空格。有没有办法删除所有这些,以便我留下一个可用的数组?

【问题讨论】:

标签: php


【解决方案1】:

使用 preg_split() 在所有换行符序列 (\R) 上进行拆分,并包含零个或多个前导或尾随空格序列 (\s*)。预先修剪输入,以防输入字符串的开头或结尾处存在不与换行符序列相邻的空格。

代码:(Demo)

var_export(preg_split('~\s*\R\s*~', trim($string), -1, PREG_SPLIT_NO_EMPTY));

输出:

array (
  0 => 'Object One',
  1 => 'Object Two',
  2 => 'Object Three',
  3 => 'Object Four',
  4 => 'Object Five',
  5 => 'Object Six',
)

【讨论】:

    【解决方案2】:

    试试这个...

    输入

     $data = "        Object One
    
    
            Object Two
    
    
            Object Three
    
            Object Four
                   Object Five
    
            Object Six";
    

    解决方案

    $result = array_filter(array_map('trim',explode("\n", $data)));
    echo "<pre>";   print_r($result);
    

    输出

    Array
    (
        [0] => Object One
        [3] => Object Two
        [6] => Object Three
        [8] => Object Four
        [9] => Object Five
        [11] => Object Six
    )
    

    【讨论】:

    • 我已经使用它并添加了 array_values() 以重新索引数组。效果很好。
    【解决方案3】:
     <?php 
     $data = " Object One
    
    
        Object Two
    
    
        Object Three
    
        Object Four
               Object Five
    
        Object Six";
    
        $data = explode("\n", $data);
        $finalArray = array();
        foreach($data as $value){
             if(!empty(trim($value))){
                $finalArray[] = trim($value);  
             }
        }
    
        print_r($finalArray);
    

    输出:

    Array ( [0] =&gt; Object One [1] =&gt; Object Two [2] =&gt; Object Three [3] =&gt; Object Four [4] =&gt; Object Five [5] =&gt; Object Six )

    【讨论】:

      【解决方案4】:

      您可以通过trim()explode() 生成的每一行,忽略修剪后为empty strings 的行。

      或者您可以使用preg_split() 将任何包含至少一个换行符的空格序列用作分隔符:

      $text = <<<E
              Object One
      
      
              Object Two
      
      
              Object Three
      
              Object Four
                     Object Five
      
              Object Six
      E;
      
      var_dump(preg_split('/\s*\n(\s*\n)*\s*/', trim($text)));
      

      输出是:

      array(6) {
        [0]=>
        string(10) "Object One"
        [1]=>
        string(10) "Object Two"
        [2]=>
        string(12) "Object Three"
        [3]=>
        string(11) "Object Four"
        [4]=>
        string(11) "Object Five"
        [5]=>
        string(10) "Object Six"
      }
      

      regex

      \s*            # zero or more (*) whitespaces (\s)
      \n             # a newline character
      (              # beginning of a group
          \s*\n      # zero or more whitespaces followed by a newline
      )              # end of the group
      *              # zero or more repetitions of the previous expression (the group)
      \s*            # zero of more whitespaces
      

      【讨论】:

        【解决方案5】:

        您可以使用array_filter 过滤掉数组中的空字符串

        array_filter(explode("\n", $output));

        要删除开头的空格,请使用array_map

        $result = array_filter(explode("\n", $output));
        array_map('trim', $result);
        

        【讨论】:

        • 应使用 PHP_EOL 而不是 "\n" 以实现跨平台兼容性,并且您需要存储 array_map 结果,即 $result = array_map('trim', $result);
        猜你喜欢
        • 2019-11-01
        • 2013-07-28
        • 1970-01-01
        • 2011-07-20
        • 1970-01-01
        • 2011-10-26
        • 1970-01-01
        • 2012-05-29
        • 2016-10-26
        相关资源
        最近更新 更多