Forum Moderators: coopster
I have a string like this
$data=
"
<NAME>ITEM1</NAME>
<VALUE>VALUE OF ITEM 1 , THIS N THAT </VALUE>
<NAME>ITEM2</NAME>
<VALUE>VALUE OF ITEM 2 , blah blah blah </VALUE>
<NAME>ITEM1</NAME>
<VALUE>VALUE OF ITEM 3 some text here too </VALUE>
<NAME>ITEM1</NAME>
<VALUE>this is VALUE OF ITEM 4 </VALUE>
";
These <NAME><VALUE> pairs in my string would be unknown untill the variable populates, sometimes there may be 10 items sometimes there may be 100 items but in same
exact format. what i am trying to do is to simply get a multi dimensional array named
$item
and some function which populates that array with this data after parsing that string, for example if string $data has 5 name,value pairs
then after execution of the code, array $items shall have 5 indexes and on each index first column contains name and second column contains value, from the string
$data.
populating the array or alternatively defining a new object to hold those values is no problem, the main thing is parsing that string to know the following
1) How many name,value pairs do we have
2) what is the value of "NAME" for each pair
3) what is the value of "VALUE" for each pair
Any assistance is highly appreciated and regarded, i think it will be done with preg_replace , not sure.
Thanks
Kami
Thanks for your question, The answer is
Actually i am getting data from a text file, it is like for example our guy at store creates a daily report of items and their values he has, he sends the report in text file, now picking up each value from that file and then sending it to database is difficult, so i thought maybe we can use those tags
<NAME>nameofitem</NAME>
<VALUE>valueoftime</VALUE>
around each item in the list and then a read whole file in a string and send to a function which returns an array of names and values so that i can parse that array and send data to mysql.
so basically the data is coming from a text file
Thanks
Kami
Following is a sample report.txt file
-------------------------------------------
<NAME>CELL PHONE</NAME>
<VALUE>SPECIAL OFFER 10 SALES</VALUE>
<NAME>WRIST WATCH</NAME>
<VALUE>NO SALES</VALUE>
<NAME>LEATHER WALLET</NAME>
<VALUE>This product is out of stock, Please Arrange.</VALUE>
<NAME>DIGGERS</NAME>
<VALUE>no demand these days</VALUE>
-------------------------------------------
Kami
----------------------------------------------
CELL PHONE,SPECIAL OFFER 10 SALES
WRIST WATCH,NO SALES
LEATHER WALLET,This product is out of stock. Please Arrange
DIGGERS,no demand these days
----------------------------------------------
Then you would do the following:
//Assign arrays
$name = array();
$value = array();//Loop through text file and assign data to arrays
$contents = file('report.txt');
for ($i=0; $i<count($contents); $i++)
{
$newdata = explode(",", $contents[$i]);
$name[] = $newdata[0];
$value[] = $newdata;
unset($newdata);
}//Show total entries in text file
echo "Total Entries: " . count($name);
//Loop through data
for ($i=0; $i<count($name); $i++)
{
echo "Name: " . $name[$i] . "<br>";
echo "Value: " . $value[$i] . "<br><br>";
}
Not tested, but something like that should work ok. (I think). ;)
dc
[1][edited by: dreamcatcher at 8:45 pm (utc) on July 30, 2005]
Many Thanks . I like your extremely logical code, there is just one concern due to which i didnt code like this on this issue, that is that <VALUE> of any item can be a full paragraph of text it can contain (')(")(,)(.) and stuff like that and it can even contain '\n' endline characters in one single value like this
<VALUE>
This Item's Value is a sample worst case value. blah blah blah.
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. 12:58:23 (AM)
</VALUE>
And another reason is that actually the file comes from another department and they have already so much files ready to be parsed that at this stage it will be unable to change the structure.
Kami
$newdata = explode(",", $contents[$i]);
would become:
$newdata = explode("¦", $contents[$i]);
I`m not really sure how to loop through a file getting the values between the tags you have. preg_match could be used to get data between two tags, but not really sure how you would loop through a text file.
This is where ergophobe, coopster or jatar_k come along to help us out. :)
preg_match("/<NAME>(.*)<\/NAME>/Uis", $string, $names);
preg_match("/<VALUE>(.*)<\/VALUE>/Uis", $string, $values);
print_r($names);
// Returns
// Array
// (
// [0] => <NAME>name of item1</NAME>
// [1] => name of item1
// )