Conditional statement

Back in the 70s my programming experience was in assembly language. So I am not programming anymore having been retired for some time. I am barely familiar with PHP and need your expert help in writing a simple text file routine that will be a help to me in my Bible study.

      I copy and paste the Scripture verse into a text file called strongs.txt and this is what the text file looks like:

Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .

This is a simple routine that I copied off the Internet and that's as far as I've been able to go and I'm not sure if it will even work for what I need to do or not:

<?php
$myfile = fopen("strongs.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
 while(!feof($myfile)) {
   echo fgetc($myfile);}

fclose($myfile);
?>

And it does print out the file strongs.txt when run in my browser.

Here is the format I need the file to be in:
Matthew 5:5 Blessed 3107 
are the meek: 4239 
for 3754 
they 846 
 shall inherit 2816 5692 
the earth 1093 .

The text file strongs.txt and the word phrase associated numbers are separated by double spaces. So I would presume some kind of conditional statement would be needed to test for the double spaces then print the line into a new file with a return to place them on separate lines. Not being familiar with PHP what would you suggest? Any coding that would complete this routine would be greatly appreciated. Hope this all makes sense,

Michael
mhoggatt1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
You could file_get_contents() function which reads the file content and saves it to a string. Then, you can use explode() to split the string depending on double spaces. Finally you iterate through the array echoing its elements.
Here you find the untested code:
$myfile = file_get_contents("strongs.txt") or die("Unable to open file!");
$arr = explode("  ", $myfile);
foreach ($arr as $a){
    echo $a."<br>";
}

Open in new window

Ray PaseurCommented:
With a background in assembly language, you will be able to pick up PHP very quickly.  But there are a lot of really bad PHP examples littering the internet.  Some of this article will be information you already know - just skip over that part.  The rest will help you find the good learning resources!
http://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

Just curious - what do the numbers in the scripture mean?
mhoggatt1Author Commented:
Thanks for the coding.
The code runs but it does not put each separate word phrase and number on a separate line sounds like the enter or return isn't working.
Here is the code:
<?php
$myfile = file_get_contents("strongs.txt") or die("Unable to open
file!");
$arr = explode("  ", $myfile);
foreach ($arr as $a){
    echo $a."<br>";}
   
fclose($myfile);
?>

thanks for your inquiry about the numbers. Some phrases only have one number associated with them and that is a Strongs number which gives the Greek meaning for the English word.
If there is another number associated with the phrase that is the tents voice and mood of a Greek verb.
We're almost there, keep the code coming !

Michael
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Ray PaseurCommented:
Can you please upload the original file or post a link to it?  Whitespace is hard to get right unless we can see the actual data.  Thanks!
mhoggatt1Author Commented:
okay let's see if I can do this. See if I can attach one of the chapters in Jude that I am working on.

Michael
Marco GasiFreelancerCommented:
Please, see here: http://webintenerife.com/testing_script/q_28919403.php
I created the file strongs.txt with your text:

strongs.txt
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .

Open in new window

nd then I used exactly the code above. Probably there is some other problem: what is the output of that code in you environment?
mhoggatt1Author Commented:
that is exactly what I am looking for. When I run script on my Windows 7 and PHP 5.6 it just prints a straight line across the browser. It doesn't separate into lines like yours does. What's next?
Michael
Dave BaldwinFixer of ProblemsCommented:
The HTML in the browsers does Not display returns, linefeeds, tabs, or more than one consecutive space unless you put the text in a <pre> block..
Ray PaseurCommented:
Use your browser's  "view source" to look at the output.
http://iconoun.com/demo/temp_mhoggatt1.php
<?php // demo/temp_mhoggatt1.php
/**
 * http://www.experts-exchange.com/questions/28919403/Conditional-statement.html
 *
 * http://php.net/manual/en/function.file-get-contents.php
 *
 * ref: http://biblehub.com/greek/3107.htm
 */
error_reporting(E_ALL);
echo '<pre>';

// USE HEREDOC NOTATION TO SIMULATE READING THE DATA WITH file_get_contents()
$txt = <<<EOD
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .
EOD;

// SHOW THE ORIGINAL STRING
echo PHP_EOL . '1...5...10...15...20...25...30...35...40...45...50...55...60...65...70...75...80....85...90...95..100';
echo PHP_EOL . $txt;
echo PHP_EOL;

// REPLACE THE DOUBLE SPACES WITH HTML '<br>' TAGS
$str = str_replace('  ', '<br>', $txt);

// SHOW THE WORK PRODUCT
echo PHP_EOL . $str;

Open in new window

mhoggatt1Author Commented:
I ran the demo script and here's what it reads.
x1...5...10...15...20...25...30...35...40...45...50...55...60...65...70...75...80....85...90...95..100
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .

Matthew 5:5 Blessed 3107
are the meek: 4239
for 3754
they 846
shall inherit 2816 5692
the earth 1093 .

what all does this mean?
Michael
gr8gonzoConsultantCommented:
Michael, this might not be relevant, but the site Blue Letter Bible (blueletterbible.org) does this already. I use that site all the time for the same purpose - studying and being able to quickly look up words in Strong's. It also has cross-referencing built-in. If you want to build this out just for your own needs, that's totally understandable, but I wanted to throw that site suggestion out there in case it solves the original need.
mhoggatt1Author Commented:
yes I have used The Blue Letter Bible from time to time. Mostly I use the Logos Bible Software on my computer. The program we're working on now will be a big help for me, I sort of have my own study style going. I teach on Rushing Wind Radio at rushingwind.airtime.pro. I know there is a way to make this little routine work, so will keep on trying.

Michael
Ray PaseurCommented:
what all does this mean?
It means that you should read the PHP code and the comments so you know what is going on.  The script shows you the original data and the transformed data.  The point of printing out the numerically indexed character scale is to be sure that we are dealing with double spaces in the delimiter fields.
http://iconoun.com/demo/temp_mhoggatt1.php

You wrote: "...this is what the text file looks like:"
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .

You wrote: "Here is the format I need the file to be in:"
Matthew 5:5 Blessed 3107
are the meek: 4239
for 3754
they 846
 shall inherit 2816 5692
the earth 1093 .

Is this not what you want?  If it's not, please clarify, thanks.
mhoggatt1Author Commented:
yes that is the format I am looking for each phrase and their associated numbers on a separate line. When I run the PHP script everything is printed out on one line across the screen.like I said PHP code is a little over my head.
When I run this code:<?php
$myfile = file_get_contents("strongs.txt") or die("Unable to open file!");
$arr = explode("  ", $myfile);
foreach ($arr as $a){
    echo $a."<br>";}
   
fclose($myfile);
?>
is there possibly something wrong with my php.ini file? With this code I need to be able to open any file I have such as a whole chapter and put them on separate lines. I know you guys will figure this one out.
gr8gonzoConsultantCommented:
When you're viewing a text file in your browser, the web browser is expecting to read HTML code. Normal text line breaks are usually just "newline" characters (sometimes they're combined with carriage return characters). Either way, newline characters are considered to be whitespace, just like tab characters, spaces, and so on.

When a web browser encounters whitespace, it simply regards it as a single space. So for example, if you created a text file that looked like this:
Hello


Michael

Open in new window

...and tried to view that in your browser, it would probably just look like:
Hello Michael

Open in new window

...without any line breaks. If you viewed the source for that page, you'd see all the extra line breaks. So there's a difference between the SOURCE of a web page, and how the browser RENDERS that source.

In order to tell the browser to render a line break on the resulting web page, it has to be given HTML code to do that. There's a variety of ways to do that, but the simplest is just to use the HTML tag <br>. So if you changed your file to:

Hello<br>
<br>
<br>
Michael

Open in new window

...then the browser would see those "<br>" tags and render the line breaks. The result would actually show something like this on the screen:
Hello


Michael

Open in new window

So what the previous experts are doing is showing you different ways of turning those double-spaces into "<br>" tags. In my opinion, using the explode/foreach is not the right way to do it, even if it works. It would be far better to use str_replace, as Ray showed in his example towards the end:

$str = str_replace('  ', '<br>', $txt);

Ray's code is trying to provide you with some before and after comparisons all in the same script so you can learn how it's working, but that str_replace is the piece that does the magic.
mhoggatt1Author Commented:
Yes I basically understand what you are talking about concerning replacing the double spaces. Being legally blind it is still a little hard for me to understand what you are saying since I have to listen to what is said through a screen reader.
If I need the string replace code where does it fit in the PHP code?
In Marco's response he said he ran PHP code and it worked properly for him so why shouldn't it work for me also?
I am not really interested in learning PHP coding or wading through a lot of documentation for just this one little program. That's why I am depending on you guys to send me the code that will work for me without having to go through a lot of debugging, etc. You guys have always come through for me before and I know you will again.
Thanks,

Michael
Marco GasiFreelancerCommented:
Which brower are you using?
Also, you can drop the fclose() statement because file_get_contents read the file and closes it after reading (returning false if the reading fails).
Anyway, the single line solution provided by Ray seems to work well even in your computer, so just use that:
$txt = file_get_contents("strongs.txt") or die("Unable to open file!");
$str = str_replace('  ', '<br>', $txt);
echo $str;

Open in new window

The reason because my code doesn't give the expected result in your browser will remain one of the many mysteries in Programming Land (and Ray solution is more concise and simple for sure) :-)
Ray PaseurCommented:
...listen to what is said through a screen reader
Ahh, now maybe we can make more progress!  Please be sure to mention the audio screen reader when it's in play with any future questions -- it may make a big difference in how we would try to answer.

I am going to guess that the screen reader would give you some kind of a "stop" if it encounters punctuation, so this may be one way of dealing with the issue.  Another way might be to insert a blank line or paragraph mark between each of the data elements, or make the data elements into a bulleted list.  If none of those methods work for you, then I think you might want to go back to the screen reader documentation and see what they recommend.

With blank line insertion:
<?php // demo/temp_mhoggatt1.php
/**
 * http://www.experts-exchange.com/questions/28919403/Conditional-statement.html
 *
 * http://php.net/manual/en/function.file-get-contents.php
 *
 * ref: http://biblehub.com/greek/3107.htm
 */
error_reporting(E_ALL);
echo '<pre>';

// USE HEREDOC NOTATION TO SIMULATE READING THE DATA WITH file_get_contents()
$txt = <<<EOD
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .
EOD;

// REPLACE THE DOUBLE SPACES WITH HTML '<br><br>' TAGS
$str = str_replace('  ', '<br><br>', $txt);

// SHOW THE WORK PRODUCT
echo PHP_EOL . $str;

Open in new window

With punctuation insertion:
<?php // demo/temp_mhoggatt1.php
/**
 * http://www.experts-exchange.com/questions/28919403/Conditional-statement.html
 *
 * http://php.net/manual/en/function.file-get-contents.php
 *
 * ref: http://biblehub.com/greek/3107.htm
 */
error_reporting(E_ALL);
echo '<pre>';

// USE HEREDOC NOTATION TO SIMULATE READING THE DATA WITH file_get_contents()
$txt = <<<EOD
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .
EOD;

// REPLACE THE DOUBLE SPACES WITH HTML '.<br>' TAGS
$str = str_replace('  ', '.<br>', $txt);

// SHOW THE WORK PRODUCT
echo PHP_EOL . $str;

Open in new window

With HTML paragraph insertion:
<?php // demo/temp_mhoggatt1.php
/**
 * http://www.experts-exchange.com/questions/28919403/Conditional-statement.html
 *
 * http://php.net/manual/en/function.file-get-contents.php
 *
 * ref: http://biblehub.com/greek/3107.htm
 */
error_reporting(E_ALL);
echo '<pre>';

// USE HEREDOC NOTATION TO SIMULATE READING THE DATA WITH file_get_contents()
$txt = <<<EOD
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .
EOD;

// REPLACE THE DOUBLE SPACES WITH HTML PARAGRAPH TAGS
$str = str_replace('  ', '</p><p>', $txt);

// SHOW THE WORK PRODUCT
echo PHP_EOL . '<p>' . $str . '</p>';

Open in new window

With paragraph-mark insertion:
<?php // demo/temp_mhoggatt1.php
/**
 * http://www.experts-exchange.com/questions/28919403/Conditional-statement.html
 *
 * http://php.net/manual/en/function.file-get-contents.php
 *
 * ref: http://biblehub.com/greek/3107.htm
 */
error_reporting(E_ALL);
echo '<pre>';

// USE HEREDOC NOTATION TO SIMULATE READING THE DATA WITH file_get_contents()
$txt = <<<EOD
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .
EOD;

// REPLACE THE DOUBLE SPACES WITH PARAGRAPH MARKERS
$str = str_replace('  ', '<br>&para; ', $txt);

// SHOW THE WORK PRODUCT
echo PHP_EOL . '&para; ' . $str;

Open in new window

With a bulleted list:
<?php // demo/temp_mhoggatt1.php
/**
 * http://www.experts-exchange.com/questions/28919403/Conditional-statement.html
 *
 * http://php.net/manual/en/function.file-get-contents.php
 *
 * ref: http://biblehub.com/greek/3107.htm
 */
error_reporting(E_ALL);
echo '<pre>';

// USE HEREDOC NOTATION TO SIMULATE READING THE DATA WITH file_get_contents()
$txt = <<<EOD
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .
EOD;

// REPLACE THE DOUBLE SPACES WITH LIST ITEM MARKERS
$str = str_replace('  ', '</li><li>', $txt);

// SHOW THE WORK PRODUCT
echo PHP_EOL . '<ul><li>' . $str . '</li></ul>';

Open in new window

HTH, ~Ray

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mhoggatt1Author Commented:
RayRay,
thank you for your code. My question is can this code be adapted to open strongs.txt ? this text file will contain a whole chapter. So the text file will vary from time to time according to what I want to unpack.
I didn't realize printing out in a browser would be so tricky. In fact I think I may have been going about this the wrong way. After I receive your answer I will accept multiple answers and close this question out.
I think what I should have been doing his reading from my text file and then writing the separate lines to a nether text file. Then I can open the entire thing in Notepad.
Thanks for all of your help from all of you guys, you really are experts!

Michael
gr8gonzoConsultantCommented:
Just take this part of Ray's code:

$txt = <<<EOD
Matthew 5:5 Blessed 3107  are the meek: 4239  for 3754  they 846  shall inherit 2816 5692  the earth 1093 .
EOD;

Open in new window


and change it to:

$txt = file_get_contents("strongs.txt");

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.