How do I change this
into this<age> 18 </age>
I can do the reverse one, but I couldn't figure out how to do the above one.
age: 18
Thanks in advance if anyone can teach me.
Moderator: General Moderators
into this<age> 18 </age>
I can do the reverse one, but I couldn't figure out how to do the above one.
age: 18
How to extract all contents out from the doc and make the pattern become
<XML header>
<doc>
<name>phpwalker</name>
<address>abc 124 washington </address>
</doc>
Code: Select all
name: phpwalker
address: abc 124 washingtonCode: Select all
<?xml version="1.0"?>
<pet>
<name>Polly Parrot</name>
<age>3</age>
<species>parrot</species>
<parents>
<mother>Pia Parrot</mother>
<father>Peter Parrot</father>
</parents>
</pet>Code: Select all
<?php
$file = 'data.xml';
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
if (preg_match_all("|<[^>]+>(.*)</[^>]+>|U", $line, $out, PREG_PATTERN_ORDER)) {
echo "it matches.<br/>";
echo $out[1][0] . ": " . $out[1][1] . "\n";
} else {
echo "it doesn't match.<br/>";
}
}
?>The above example is what I get from the PHP.net manual.it doesn't match.
it doesn't match.
it matches.
Polly Parrot: it matches.
3: it matches.
parrot: it doesn't match.
it matches.
Pia Parrot: it matches.
Peter Parrot: it doesn't match.
it doesn't match.
I'm still figuring thePolly Parrot:
3:
parrot:
Pia Parrot:
Peter Parrot:
Code: Select all
(preg_match_all("|<[^>]+>(.*)</[^>]+>|U", $line, $out, PREG_PATTERN_ORDER))I cant understand...This modifier inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by "?". It is not compatible with Perl. It can also be set by a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).
Code: Select all
echo $out[1][0] . ": " . $out[1][1] . "\n";Array
(
[0] => Array
(
[0] => Polly Parrot
)
[1] => Array
(
[0] => Polly Parrot
)
)
Array
(
[0] => Array
(
[0] => 3
)
[1] => Array
(
[0] => 3
)
)
Array
(
[0] => Array
(
[0] => parrot
)
[1] => Array
(
[0] => parrot
)
)
Array
(
[0] => Array
(
[0] => Pia Parrot
)
[1] => Array
(
[0] => Pia Parrot
)
)
Array
(
[0] => Array
(
[0] => Peter Parrot
)
[1] => Array
(
[0] => Peter Parrot
)
)
Ehm, I know what should I do now! Thanks feyd! I learn a lot from you.<pre>Array
(
[0] => Array
(
[0] => <name>Polly Parrot</name>
)
[1] => Array
(
[0] => Polly Parrot
)
)
</pre><pre>Array
(
[0] => Array
(
[0] => <age>3</age>
)
[1] => Array
(
[0] => 3
)
)
</pre><pre>Array
(
[0] => Array
(
[0] => <species>parrot</species>
)
[1] => Array
(
[0] => parrot
)
)
</pre><pre>Array
(
[0] => Array
(
[0] => <mother>Pia Parrot</mother>
)
[1] => Array
(
[0] => Pia Parrot
)
)
</pre><pre>Array
(
[0] => Array
(
[0] => <father>Peter Parrot</father>
)
[1] => Array
(
[0] => Peter Parrot
)
)
</pre>