[SOLVED] Theory Question

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

[SOLVED] Theory Question

Post by Sphen001 »

Hi,

I have a question for all you PHP experts.

I'm trying to parse a file and add it to an array.

The file would look similar to this.

Code: Select all

<mod>Some mod</mod>
<title>A title</title>
And so on and so on, with many different things between the brackets. What I'm trying to do is to parse the file and, say for the example above have something like:

Code: Select all

echo $mods['mod'] = 'Some mod';
echo $mods['title'] = 'A title';
I'm trying to find a way that I can do this. I know I'll need to use Regex, but I would like some input from you if you can see a better way I could do this. All I'm looking for are ideas...I think that I can code this myself, but it's the theory I can't quite get.

Thanks a lot

Sphen001
Last edited by Sphen001 on Tue Jul 12, 2005 9:24 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I don't know what you mean with "theory" in this case..

I would solve it as following:

Each line has a < then characters1 then > then characters2 then < / characters1 again and >.

If Regular Expressions are to hard to grasp, you could use in this case sscanf(). Something like

Code: Select all

list($name, $data, $name2) sscanf("<%s>%s</%s>", $line);
( JAM | Fixed the link. )
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Thanks for introducing me to a new function! Very Cool! 8)
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Thanks for the reply.

Maybe I didn't make the question clear enough...

I'm going to give this another think through, and I'll post a more clear description later.

Thanks for your time.

Sphen001
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Are you talking about XML parsing?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Complete code would look like: (untested | you should lookup which characters need escaping in the regular expression, i think it's / and >)

Code: Select all

$mods = array();
$lines = file_get_contents('somefile.txt');
foreach($lines as $line)
{
  //list($name1, $value, $name2) = sscanf($line, "<%s>%s</%s>");
  //$mods[$name1] = $value;
  preg_match("#<(.*?)>(.*?)</(.*?)>#", $line, $matches);
  $mods[$matches[1]] = $matches[2];
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I hope he's not... Because the sample he showed was not valid XML ;)
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Ok,

Tim, thanks for the code. It's going to be very helpful.

No, it's not XML.

I'm developing a forum type system, and we want one of the features to be able to automatically install MOD's. There will be an install file for each MOD, and it will contain such markup as:

Code: Select all

<mod>This is a MOD</mod>
<title>The MOD title</title>
<open>Somefile.txt</open>
<find type="partial">if ($config['load_activity</find>
<replace>// if ($config['load_activity</replace>
The problem is that I want to store this in one array. ie.

$edits['mod'] value is "This is a MOD"

However, I can see some potential problems, so I posted here if anyone could think of a better way for me to do this.

If you have any thoughts, I would love to hear them.

Thanks again

Sphen001
Parazuce
Forum Newbie
Posts: 1
Joined: Sun May 29, 2005 3:56 pm
Location: USA

Post by Parazuce »

Ok, here's a code I created, took about 3 minutes, and it's just scratchup, but works.

However, it does not support your values inside the element, for example, it wont support this:

<element value="Here's the value">

It will just take all of that and create one big element, like this:
element value="Here's the value"

Here's the code:

Code: Select all

<?php

$file = "<mod>This is a MOD</mod>\n"
       ."<title>The MOD title</title>\n"
       ."<open>Somefile.txt</open>\n"
       ."<find type=\"partial\">if (\$config['load_activity</find>\n"
       ."<replace>// if (\$config['load_activity</replace>";

$file_array = explode( "\n", $file );

for( $i = 0; $i < count( $file_array ); $i++ )
{
	 $element[$i] = preg_replace( ",<([a-zA-Z0-9 \"'=]*?)>(.*)</([a-zA-Z0-9 \"'=]*?)>,", '\\1', $file_array[$i] );
     $values[$i]  = preg_replace( ",<([a-zA-Z0-9 \"'=]*?)>(.*)</([a-zA-Z0-9 \"'=]*?)>,", '\\2', $file_array[$i] );

     $edits[$element[$i]] = $values[$i];
}
?>
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Best thing would be to use a backreference for the closing tag, as both <abc>123</abc> and <abc>123</def> would match. Something like this;

Code: Select all

#<(&#1111;a-zA-Z0-9_\-]*?)>(.*?)</\\1>#s
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Or prepend a '<root>' and append a '</root>' to the string.

And start using the XML parsing functions :)) At first they might seem a bit scary, but in this XMLized world you'll have to get used to them anyway...
Post Reply