Page 1 of 1

Parsing a file for data.

Posted: Tue Mar 20, 2007 5:59 pm
by 303tech
I would like php to open a local text file and parse it to find the first instance of "phonenumber=<withgodknowswhatnumberhere>" and put only the number into a string.

I think I know how to get it to find the first instance of with strpos(), but I see the problem as determining how to define where the copying should end since not every text file will have the same order of init setup in the text.

Thanks for your help.[/quote]

Posted: Tue Mar 20, 2007 6:03 pm
by Chris Corbyn
Have a snoop around in here: viewforum.php?f=38

Maybe read the stickies at the top of the forum :) Regex is what you need.

Posted: Wed Mar 21, 2007 12:25 pm
by 303tech
would it be easier to store the text file in an array since each line has a return (line break)

storeid=nicks house of potatoes
phonenumber=303-555-5555
address =123 abc way

------

if so, would it store something like (obviously not in the correct syntax but i think youd get the idea). using fopen(cft.txt)

array [1] storeid=nicks house of potatoes
[2] phonenumber=303-555-5555
[3] address =123 abc way

and then parse the array?

Posted: Wed Mar 21, 2007 12:39 pm
by feyd

Posted: Wed Mar 21, 2007 4:53 pm
by 303tech
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


okay so far ive got...

Code: Select all

<?php
$contents = file_get_contents("Aloha.ini");
$pos=strpos($contents, "UNITNAME=");
echo substr($contents, $pos, 40);
?>

which displays

UNITNAME=ROSIE'S DINER ADDRESS1=14061 E

Problem is that i cant have the next config line in there. It may be address=, or it may be phone=

no matter, each one may have a varied char length.

i guess the next question is, how do I minus out UNITNAME=, and next config= line? I dont think i can define it to say to stop at the next blank space, becuase there may be spaces in the name itself, and never a set amount of course. Out of a thousand customers, each one definately does not have exactly the same order of config.
Sorry this is so newb, im just trying to learn is all.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Mar 21, 2007 5:38 pm
by 303tech
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


okay i got as far as:

Code: Select all

<?php
$contents = file_get_contents("Aloha.ini");
$pos=strpos($contents, "UNITNAME=");
$string=substr($contents, $pos, 40);

$pos2=strpos($string, "="); 

echo substr($string, 9, 25);
?>
which returns.

ROSIE'S DINER ADDRESS1=1

...im almost there, just now i can't get the address1=1 out .----or----in other cases would be phone=( or storeid=4. They are never in any order.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Mar 21, 2007 5:40 pm
by 303tech
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


crap, so i just did

Code: Select all

<?php
$ini = parse_ini_file('Aloha.ini');
$rosies = $ini['UNITNAME'];
echo $rosies;
?>
and get what i need...however if i want to do phone number......since they all have () and -'s in them. how would i remove these?

i get...
Warning: Error parsing Aloha.ini on line 180 in /home/tech303/public_html/php/insert.php on line 2
303

not sure what the error is from, but it seems to parse fine.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Mar 21, 2007 6:25 pm
by harrisonad
use file() instead of file_get_contents() to read the file and returns every line as array.
then use explode to separate the keyword from value.

Posted: Wed Mar 21, 2007 9:48 pm
by feyd
Have you tried parse_ini_file()?