I need to scrub some data from a log of some xml and I need to know how to make preg_replace replace all of the occurrences of a regex match.
The xml looks something like this.
<contactData>
<id>29194</id>
<firstName>Michael</firstName>
<lastName>Smith</lastName>
<address1>1600 Pennsylvania Ave</address1>
<address2></address2>
<city>Washington</city>
<state>DC</state>
<postalCode>20500</postalCode>
<country>US</country>
<phone>3012013021</phone>
<email>michael@potus.gov</email>
</contactData>
<contactData>
<id>29195</id>
<firstName>Shelly</firstName>
<lastName>McPherson</lastName>
<address1>2411 Georgia Ave</address1>
<address2></address2>
<city>Silver Spring</city>
<state>MD</state>
<postalCode>20902-5412</postalCode>
<country>US</country>
<phone>3012031302</phone>
<email>shelly@example.com</email>
</contactData>
When I run this on this xml.
$regex = $replace = array();
$regex[] = '/(<contactData>)(.*)(<email>)(.*)(<\/email>)/is';
$regex[] = '/(<contactData>)(.*)(<phone>)(.*)(<\/phone>)/is';
$replace[] = '$1$2$3xxxxxxxxxxxxxxxx$5';
$replace[] = '$1$2$3xxxxxxxxxxxxxxxx$5';
$text = preg_replace($regex, $replace, $text);
I get this.
<contactData>
<id>29194</id>
<firstName>Michael</firstName>
<lastName>Smith</lastName>
<address1>1600 Pennsylvania Ave</address1>
<address2></address2>
<city>Washington</city>
<state>DC</state>
<postalCode>20500</postalCode>
<country>US</country>
<phone>3012013021</phone>
<email>michael@potus.gov</email>
</contactData>
<contactData>
<id>29195</id>
<firstName>Shelly</firstName>
<lastName>McPherson</lastName>
<address1>2411 Georgia Ave</address1>
<address2></address2>
<city>Silver Spring</city>
<state>MD</state>
<postalCode>20902-5412</postalCode>
<country>US</country>
<phone>xxxxxxxxxxxxxxxx</phone>
<email>xxxxxxxxxxxxxxxx</email>
</contactData>
How do I get it replace the other "contactData" email and phone?