0

I'm trying to make a plaintext file into an array (they are already organized by verses). I want to have each verse as an item on the array. My current code only returns the final verse in the text file. The print_r proves that it thinks that the final verse is the only item in the array. I'm missing something obvious, what is it?

Code:

<?php

$url = 'homily1burgundio.txt';

$homily = file($url);

foreach ($homily as $line) {
    $line_array = preg_split( '/\r\n|\r|\n/', $line );
}
echo "<p>" . $line_array[0] . "</p>";
print_r($line_array);
?>
        </body>

Sample text:

1.4.9 legatus nobis uenit de celo ab ipso missus Deo de quibusdam necessariis disputaturus. Deinde nos dimittentes audire quid uult, et quid legationis nuntiat uobis, sedetis mimos audientes, et quot non digna hec fulminibus et fulguribus? 

1.4.10 Sicut enim mensa non oportet participare demoniorum, ita neque auditione demoniorum, neque cum sordida ueste ad preclaram mensam uenire que tot plena est bonis et quam ipse preparauit Deus. Tanta enim eius est uirtus, ut et ad celum ipsum repente nos eleuet, solum si sobria mente attendamus. 

1.4.11 Non enim est eum qui continue diuinis presonatur sermonibus, in presenti manere humilitate. Sed necesse leuari confestim, et ad ipsam euolare superiorem regionem, et infinitis potiri bonorum thesauris que fiat omnes nos promereri gracia et clementia Domini nostri Ihesu Christi per quem et cum quo Patri est gloria simul cum Sancto Spiritu nunc et semper et in secula seculorum. Amen.
1
  • Why don't you try to use file_get_contents together with explode Commented Dec 28, 2016 at 7:34

2 Answers 2

1

It could be because you loop through lines, but $line_array is just a variable. if you make it $line_array[] (like below), it would work.

foreach ($homily as $line) {
    $line_array[] = preg_split( '/\r\n|\r|\n/', $line );
}

You will have a array for $line_array.

Note: I did not check this, this should works. Thanks!


Update

According to your comment I add some validation for the code, As you need lines which has text and not the empty lines, the below code do the trick for you.

<?php
$url = 'homily1burgundio.txt';
$homily = file($url);
foreach ($homily as $line) {
    $result = array_filter(preg_split( '/\r\n|\r|\n/', $line ));
    if ( isset($result[0]) )
        $line_array[] = $result[0];
    }
//echo "<p>" . $line_array[0] . "</p>";
echo "<pre>".print_r($line_array, true)."</pre>";
?>

Basically this filters out the empty lines from the file and if only there is a valid line, it store that line to $line_array

Hope this work for you! Thanks!

Sign up to request clarification or add additional context in comments.

5 Comments

Hmm, it creates an array that looks like this: Array ( [0] => [1] => ) [91] => Array ( [0] => 1.4.11 Non enim est eum qui continue diuinis presonatur sermonibus, in presenti manere humilitate. Sed necesse leuari confestim, et ad ipsam euolare superiorem regionem, et infinitis potiri bonorum thesauris que fiat omnes nos promereri gracia et clementia Domini nostri Ihesu Christi per quem et cum quo Patri est gloria simul cum Sancto Spiritu nunc et semper et in secula seculorum. Amen. ) )
Hmm, it creates an array that begins like this: Array ( [0] => [1] => ) [91] => Array ( [0] => 1.4.11. This is a multi-dimensional array, yes? How do I echo each item in the array?
What is your expected output?
My expected output is "1.11 etc." I'd like to only output the first item in the array. It's currently spitting out "Array ( [0] => [1] => ) [91] => Array ( [0] =>" before the text, which I'd like to avoid. Is this possible?
Just tried your edit. You are a saint! Everything works as I had hoped now. Thanks for the commented out code as well. Happy new year!
0
$url = 'homily1burgundio.txt';

$homily = file($url);
$lines = preg_split( '/\n/', $homily );
//you got every line as an element of the array
var_dump($lines);

if you want more specific for a single line do this

$multi_dim_array=array();
foreach ($lines as $line) {
    $multi_dim_array[] = preg_split( '/\r\n|\r|\n/', $line );        
}
var_dump($multi_dim_array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.