Parsing a file for data.
Moderator: General Moderators
Parsing a file for data.
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]
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]
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
Maybe read the stickies at the top of the forum
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?
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?
feyd | Please use
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]
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]feyd | Please use
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]
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);
?>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]feyd | Please use
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]
crap, so i just didCode: Select all
<?php
$ini = parse_ini_file('Aloha.ini');
$rosies = $ini['UNITNAME'];
echo $rosies;
?>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]- harrisonad
- Forum Contributor
- Posts: 288
- Joined: Fri Oct 15, 2004 4:58 am
- Location: Philippines
- Contact:
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Have you tried parse_ini_file()?