如何清理PHP换行符

axkjgtzd  于 2023-04-28  发布在  PHP
关注(0)|答案(5)|浏览(151)

我需要清理以下PHP数组并删除所有空变量。该数组是由从第三方软件复制的字符串组成的,包含许多换行符。
下面是我尝试和失败的方式。

数组:

Array ( 
[0] => 
[1] => 1.BOB/SMITH RANI MRS
[2] => 2.JON/SMITH MSTR(CHD/01NOV01) 
[3] => 
[4] => 3.BOB/JONES MSTR(CHD/01JUN01)
[5] => 4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2 
[6] => 5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 
[7] => 6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 
[8] => 7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2 )

我尝试过但失败的函数:

public static function filter_array_empty_value($arr){
    foreach($arr as $k=>$v){
        
        $v = str_replace("\n","",$v);
        $v = str_replace(" ","",$v);
        $v = trim($v);
        
        for($i=1;$i<=50;$i++){
            $v = str_replace("\n","",$v);
            $v = str_replace(" ","",$v);
        }
        
        if($v == " " || $v == NULL || empty($v) || strlen($v)<1){
            unset($arr[$k]);
        }
    }

    return $arr;
}

**注意:**数组[0]在修剪后字符串长度仍为4。

感谢帮助。

arknldoa

arknldoa1#

假设您的行数组是file()函数的结果,只需使用own file函数支持的标志即可。就像这样:

$filelines = file('filename.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES)

使用可选标志时,FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES文件函数将返回除空行以外的所有行

jvlzgdj9

jvlzgdj92#

$arr = array ( 
    0 => "",
    1 => "1.BOB/SMITH RANI MRS",
    2 => "2.JON/SMITH MSTR(CHD/01NOV01) ",
    3 => "   ",
    4 => "3.BOB/JONES MSTR(CHD/01JUN01)",
    5 => "4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2\n ",
    6 => "5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 ",
    7 => " 6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 ",
    8 => "7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2", 
); 

function filter_array_empty_value($arr){
    foreach($arr as $k=>$v){
        $v = str_replace("\n","",$v);
        $v = str_replace(" ","",$v);
        $v = trim($v);
        $arr[$k] = $v;

        if($v == ""){
            unset($arr[$k]);
        }
    }
    return $arr;
}

$arr2 = filter_array_empty_value($arr);

print_r($arr2);

输出为:

Array
(
    [1] => 1.BOB/SMITHRANIMRS
    [2] => 2.JON/SMITHMSTR(CHD/01NOV01)
    [4] => 3.BOB/JONESMSTR(CHD/01JUN01)
    [5] => 4EK004U26JUL6*LHRDXBHK32040063027JULEEK/GAQNL2
    [6] => 5EK584U27JUL7*DXBDACHK31315195527JULEEK/GAQNL2
    [7] => 6EK583U09SEP2*DACDXBHK31015130509SEPEEK/GAQNL2
    [8] => 7EK005U09SEP2*DXBLHRHK31545201509SEPEEK/GAQNL2
)
v7pvogib

v7pvogib3#

你可能应该看看你是如何分割字符串以获得数组并在那里做的,但这很简单:

$array = preg_grep('/[^\s]+/', $array);

如果还有其他不可见字符,则将其添加到字符类[]中。
如果需要重新索引:

$array = array_values(preg_grep('/[^\s]+/', $array));
vc9ivgsu

vc9ivgsu4#

没有什么比这更简单的了:

public static function filter_array_empty_value($arr){
    //Output Array
    $output = [];

    //Iterate through each item
    foreach($arr as $item){
        //Make sure, all not printable Chracters are removed (If you want UTF-8 Characters instead of asci use "/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u"
        $item = preg_replace('/[^[:print:]]/', '', $item);
        $item = trim($item);

        //If item is not empty, append it to the Output
        if ($item !== '') {
            $output[] = $item;
        }
    }
    return $output ;
}

如注解中所述,我们需要删除不可打印的字符:
https://stackoverflow.com/a/8171868/2441442

zvms9eto

zvms9eto5#

使用array_filter删除数组中的空值
要重新创建顺序索引,请使用array_values,如下所示

$arr =Array ( 
"0" => "",
"1" => "1.BOB/SMITH RANI MRS",
"2" => "2.JON/SMITH MSTR(CHD/01NOV01) ",
"3" => "",
"4" => "3.BOB/JONES MSTR(CHD/01JUN01)",
"5" => "4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2 ",
"6" => "5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 ",
"7" => "6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 ",
"8" => "7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2 ");

print_r(array_values(array_filter($arr)));

输出

Array
(
    [0] => 1.BOB/SMITH RANI MRS
    [1] => 2.JON/SMITH MSTR(CHD/01NOV01) 
    [2] => 3.BOB/JONES MSTR(CHD/01JUN01)
    [3] => 4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2 
    [4] => 5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 
    [5] => 6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 
    [6] => 7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2 
)

相关问题