Using PHP regular expressions to extract a value from an XML Schema
Using开发者_StackOverflow社区 PHP Regular Expression I want to extract some value from code
<?xml version="1.0" encoding="UTF-8"?>
<xs:restriction base="xs:string">
<xs:enumeration value="001">
<xs:annotation>
<xs:documentation>Accountancy</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="002">
<xs:annotation>
<xs:documentation>Advertising</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="005">
<xs:annotation>
<xs:documentation>Amusement</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:schema>
I want to extract value 001 from
<xs:enumeration value="001">
For all tag
And text 'Accountancy' from
<xs:documentation>Accountancy</xs:documentation>
Using php reg ex match
how do I do this?
Try this for the first.
preg_match("/<xs:enumeration value=\"(\d+)\">/", $string, $matches);
print_r($matches);
And for the second
preg_match("/<xs:documentation>(.+)</xs:documentation>/", $string, $matches);
print_r($matches);
but XML parser would be a better solution. DOMDocument can handle it I think.
精彩评论