Page 1 of 1
[SOLVED] Theory Question
Posted: Sat May 28, 2005 1:41 pm
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
Posted: Sat May 28, 2005 2:19 pm
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. )
Posted: Sat May 28, 2005 8:01 pm
by neophyte
Thanks for introducing me to a new function! Very Cool!

Posted: Sat May 28, 2005 8:51 pm
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
Posted: Sat May 28, 2005 9:10 pm
by Ambush Commander
Are you talking about XML parsing?
Posted: Sun May 29, 2005 3:57 am
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];
}
Posted: Sun May 29, 2005 3:58 am
by timvw
I hope he's not... Because the sample he showed was not valid XML

Posted: Sun May 29, 2005 8:57 am
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
Posted: Sun May 29, 2005 4:10 pm
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];
}
?>
Posted: Mon May 30, 2005 11:41 am
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
#<(їa-zA-Z0-9_\-]*?)>(.*?)</\\1>#s
Posted: Mon May 30, 2005 12:37 pm
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...