Jump to content

Can You Do Something Like This In Php?


danny_woo

Recommended Posts

Hi people,

 

I'm still going through the long learning curve of understanding of PHP so please go easy on me!

 

Right now I'm going through a course to help me on my way and i have recently gone through a lesson which briefly covered the 'implode" and 'explode' functions in regards to an array.

 

Anyway, I have been thinking and was wondering if I could somehow use PHP to clear up some data for my own personal use.

 

What i mean is, I have a list of data, lets say it looks like this:

 

#John Smith

1.5.55.1.101

 

#Ben Chen

1.7.8.55.4.1

 

#Tim Roberts

155.587.5.5

 

#Terry Noble

9.44.47.7.6

 

#Harry Flint

9.5.44.10.7

 

...And so on. Maybe about 100 entries in total.

 

Is there a way i could write a php script that i can add this data to or that the script could fetch this data and then remove all the information except for the numbers at the bottom of each entry and then echo the result to my local host so it looks something like this:

 

1.5.55.1.101

1.7.8.55.4.1

155.587.5.5

9.44.47.7.6

9.5.44.10.7

 

This is really just an expirement, if it is possible I would love to know where to start, so i can start playing around with this. Again If it is possibe what i would like to do eventually is add a function that removes duplicates.

 

The list of data is not on a database or anything, its just on a text file and I dont really wanna put it on a database. I guess i could add it to the top of my php script right as some kind of string? Or am i going off track?

 

I hope some one can point me in the right direction, so I have something to work with and understand.

 

Thanks in advance guy's and girls.

Link to comment
Share on other sites

ok cool,

 

thanks for your quick reply man.

 

Can you give me a quick example of using fopen(), fgets() and (intval) with what i want to do please?

 

You dont have to write the whole code if its a hassle, but could you give me a rough starting point please mate. I'll read into it later to get a better idea.

 

Thank you very much for your help.

Link to comment
Share on other sites

I shouldn't have said intval, It should be is_numeric(), but anyway, it would look a little like this :

<?php
$file = fopen('/path/to/filename.txt'); //open the file
$line =""; //initialise the variable
while($line != "EOF"){ //while not the end of the file do the following
$line = fgets($file); //get the current line of the file, moving the pointer to the next line
if (is_numeric(substr($line, 0, 1))){ //check if the first character returned from that line is a number
$outputArray[] = $line; // if it is add that line to the output array
}
}
foreach($outputArray as $lineToOutput){
echo $lineToOutput;
}
?>

This is oversimplified, I don't really work with files in my code, but it's the basic logic behind what you would do.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.