Programmer in diapers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ishantic
Forum Newbie
Posts: 2
Joined: Sat Oct 16, 2010 5:50 pm

Programmer in diapers

Post by Ishantic »

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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Programmer in diapers

Post by Benjamin »

I was expecting to hear a story how you were programming in diapers. Epic fail man.

Look into the fgets() and explode() functions.
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

Re: Programmer in diapers

Post by Bind »

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:

Code: Select all

<?php
$data_array = explode(' ',file_get_contents('myfile.txt'));
echo $data_array[3];
?>
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:

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;
         }
   }
?>
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.
Ishantic
Forum Newbie
Posts: 2
Joined: Sat Oct 16, 2010 5:50 pm

Re: Programmer in diapers

Post by Ishantic »

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.
Post Reply