Forum Moderators: coopster

Message Too Old, No Replies

preg_replace, parsing a string for string pairs

         

Anyango

5:03 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Folks

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

Anyango

6:48 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe considering this script as xml and parsing this using XML functions can help?

dreamcatcher

7:44 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Kami,

The first thing we need to know before we can help you is how are you pulling the data to populate the array? You say the amount will vary. Is is coming from a database?

dc

Anyango

7:55 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi DC

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

Anyango

8:01 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and a clarification, Array is not already there, we just have a string that is it, we need to create array from that string

Thanks

dreamcatcher

8:04 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One more question, how does the data appear in the text file? Can you show an example?

dc

Anyango

8:16 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Dc

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

dreamcatcher

8:29 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, well firstly, you might want to think about restructuring the data to be a comma delimited file like this:

----------------------------------------------
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]

Anyango

8:39 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DC

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

dreamcatcher

8:51 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, with the comma delimited file you could instead use a pipe, which would eliminate the problem there. So that:

$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. :)

Anyango

8:56 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Agreed

how to get data in 2 strings

$name
$value

using preg match on this string

$string="
<NAME>name of item1</NAME>
<VALUE>value of item1</VALUE>
";

if we just consider we only have this string with the data for only one item. what preg match code can we use to populate those two variables

Kami

coopster

1:04 pm on Aug 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What do you have for your best attempt so far? It seems straightforward enough ...
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
// )

If the data in the file follows some form of logical pattern you could develop a more complex regular expression.