danny_woo Posted November 22, 2012 Share Posted November 22, 2012 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. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted November 22, 2012 Share Posted November 22, 2012 it is possible, and fairly easy as well. you wouldn't need to use implode or explode. using fopen(), fgets() and a simple (intval) would let you strip out all lines that do not start with a number. Alternativly you could use file() and then implode/explode til your hearts content. either way should be fairly simple. Quote Link to comment Share on other sites More sharing options...
danny_woo Posted November 22, 2012 Author Share Posted November 22, 2012 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. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted November 22, 2012 Share Posted November 22, 2012 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. Quote Link to comment Share on other sites More sharing options...
danny_woo Posted November 23, 2012 Author Share Posted November 23, 2012 Thanks for posting the code mate. I will play around with it this weekend and do some reading up on what you have given me. Thanks for helping the newbe out. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted November 23, 2012 Share Posted November 23, 2012 no problem, everyone starts somewhere. I got (and still get) a lot of help and info from this site so try to give back where I can. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.