El_Matella El_Matella - 3 months ago 25
PHP Question

Exploding txt file PHP

I am having a problem exploding a txt file in PHP. Here is an example of what I want to do:

Product N°3456788765
price: 0.09
name: carambar


Product N°3456789
price: 9
name: bread


So basically, I would like to have an array like:

array
[0] =>
[0] => Product N°3456788765
[1] => price: 0.09
[2] => name: carambar
[] =>
[0] => Product N°3456789
[1] => price: 9
[2] => name: bread


In the others questions, they used the explode function. Unfortunately, I don't know what to say to the function because delimiters are blank lines here...

I tried to make some research because when I go with a
strlen()
on a blank line, it shows 2 caracters. So after using
ord()
function, I saw that these two caracters were 13 and 10 in Ascii mode, but if I try a

$string = chr(13) . chr(10);
strcmp($string,$blankline);


It just doesn't work. I would have loved to use this
$string
in my explode delimiter...

Thank you all for you advices, first post here after many years finding answers :)

Answer

The result is here:

    $text = file_get_contents('file.txt');
    $temp = explode(chr(13) . chr(10) . chr(13) . chr(10),$text);
    $hands = array();
    foreach($temp as $hand){
        $hand = explode(chr(13) . chr(10),$hand);
        $hand = array_filter($hand);
        array_push($hands,$hand);
        $hand = array_filter($hand);
    }
    dd($hands);

I have two chr(13) . chr(10) when the product changes and one when it just changes the line. So it's working now!

Comments