Hi guys,
I've just started learning how to program in PHP and I've hit a bit of a wall. I'm trying to create a function that will read a line of text from a .txt file and from that line single out a numerical value.
Example:
Course ABCD CourseName 4 hours/weekly
From this what I want the function to do is to simply extract the course hours. ex: function GetCourseHours($courseLine)
The only thing I can think of is using the trim function to take out all of the letters and just leave "4", but I'm sure that there must be a better way of doing this.
Any suggestions?
Thanks.
Programmer in diapers
Moderator: General Moderators
Re: Programmer in diapers
I was expecting to hear a story how you were programming in diapers. Epic fail man.
Look into the fgets() and explode() functions.
Look into the fgets() and explode() functions.
Re: Programmer in diapers
there are a few ways to do it depending on the consistency of the data being parsed.
if its consistent number of space-delimited words with the value you want is always represented in the exact same location in the string (array position 4 in this case which is represented by array[3]), the following will work:
if the number of space delimited words are unknown but the only numeric value in the string is the number you need, you could do it like this:
now if the string data has less consistency we need to use other methods like string searches between known characors or words, or regular expression matches.
additionally if you are pulling data from sections of the file, other methods may need to be implimented, and some trimming/stripping may need to be implimented importing file data containing multiple lines.
Additional information can be found - http://php.net/manual/en/function.file-get-contents.php
note: file_get_contents() is preferred over fopen(), fgets(), fclose() because it utilizes operating systems memory mapping functionality if the operating system supports it and is generally less resource consuming.
if its consistent number of space-delimited words with the value you want is always represented in the exact same location in the string (array position 4 in this case which is represented by array[3]), the following will work:
Code: Select all
<?php
$data_array = explode(' ',file_get_contents('myfile.txt'));
echo $data_array[3];
?>
Code: Select all
<?php
$data_array = explode(' ',file_get_contents('myfile.txt'));
foreach($data_array as $this_word)
{
if(is_numeric($this_word))
{
echo $this_word;
break;
}
}
?>additionally if you are pulling data from sections of the file, other methods may need to be implimented, and some trimming/stripping may need to be implimented importing file data containing multiple lines.
Additional information can be found - http://php.net/manual/en/function.file-get-contents.php
note: file_get_contents() is preferred over fopen(), fgets(), fclose() because it utilizes operating systems memory mapping functionality if the operating system supports it and is generally less resource consuming.
Re: Programmer in diapers
Thanks so much for the help! I'm still new to programming but enjoy the learning process very much. I'll be sure to put your helpful tips to use. The program is working as it should now - thanks again.