A.Rouze
A.Rouze

Reputation: 51

preg_replace / str_replace : Inline / line break

I'm looking for an exemple to replace all my spaces to an "inline" i think in english it's line break.

This an exemple of my CSV file:

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

First, i decide to replace all my '/"/' to spaces (' '), like that:

foreach( $tableau as $row )  {   

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

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

    $i += 1;
}

This is the result:

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 

As you can see, i want (and i prefer) to do an inline. Because you can see some number like "654XXX" are next to "ne circule pas", and for me, i need these number are inline.

Anyone have a solution ? I had test something like this:

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

This is wrong, and I think that's a big mistake.

Thanks

Upvotes: 0

Views: 239

Answers (2)

Naga
Naga

Reputation: 2168

Edited: You can achive your result this way as well

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

Upvotes: 0

Toto
Toto

Reputation: 91518

You just need to replace the line break (ie. \r\n) by <br>:

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

\R stands for any kind of linebreak.
I also remove the use of $i as it is not needed because in the for loop $tableau[$i] is the same as $row

Upvotes: 2

Related Questions