preg_replace / str_replace:内联/换行符

时间:2016-09-27 09:35:33

标签: php regex csv preg-replace str-replace

我正在寻找一个例子来将我的所有空格替换为“内联”我认为在英语中它是换行符。

这是我的CSV文件的例子:

    "6543945";"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";"5017";"EnginSouhaite";"D";"1";"ne circule pas"
"6543945";"1111111111111111111111111111111111111111111111111100001010000101000010100001010000101000010100001010000101100010100001010000101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110001111111111111111111111111111111111111111000000000000000000000000000000000000";"5017";"EnginSouhaite";"D";"1";"Q JUSQU'AU 30/01/16 ; VE DF DU 31/01 AU 15/04/16 ; Q A PARTIR DU 17/04/16 (NE CIRCULE PAS 29,30,31/10/16)"
"6543946";"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";"5017";"EnginSouhaite";"D";"1";"ne circule pas"
"6543946";"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";"5017";"EnginSouhaite";"D";"1";"ne circule pas"
"6543945";"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";"5017";"EnginSouhaite";"D";"2";"ne circule pas"
"6543946";"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";"5017";"EnginSouhaite";"D";"2";"ne circule pas"

首先,我决定将所有'/“/'替换为空格(''),如下所示:

foreach( $tableau as $row )  {   

    $replace = preg_replace('/"/', ' ', $tableau[$i]);

    echo $replace; ?> <br/> <?php 

    $i += 1;
}

结果如下:

6543945 
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
5017 
EnginSouhaite 
D 
1 
ne circule pas 6543945 
1111111111111111111111111111111111111111111111111100001010000101000010100001010000101000010100001010000101100010100001010000101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110001111111111111111111111111111111111111111000000000000000000000000000000000000
5017 
EnginSouhaite 
D 
1 
Q JUSQU'AU 30/01/16 
VE DF DU 31/01 AU 15/04/16 
Q A PARTIR DU 17/04/16 (NE CIRCULE PAS 29,30,31/10/16) 6543946 

正如你所看到的,我想(而且我更喜欢)做一个内联。因为你可以在“ne circule pas”旁边看到像“654XXX”这样的数字,对我来说,我需要这些数字是内联的。

有人有解决方案吗?我测试过这样的东西:

$replace2 = preg_replace(' ', '/$\n/',$replace);

这是错的,我认为这是一个很大的错误。

由于

2 个答案:

答案 0 :(得分:2)

您只需要通过\r\n替换换行符(即。<br>):

foreach( $tableau as $row )  {   
    $replace = preg_replace('/"/', ' ', $row);
    $replace = preg_replace('/\R/', '<br>', $replace);
    echo $replace, '<br>';
}

\R代表任何类型的换行符 我也删除了$i的使用,因为在for循环中$tableau[$i]$row

相同

答案 1 :(得分:0)

编辑: 您也可以通过这种方式获得结果

foreach( $tableau as $row )  { 
    $replace = preg_replace('/"/', ' ', $row);        
    $replace_arr = explode (';',$replace);
    foreach ($replace_arr as $key=>$val){
        echo $val."<br />";
    }
}