Halfstop Halfstop - 6 months ago 41
PHP Question

PHP preg_replace all instance matches

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?

Answer

It's more correct to use any XML parser to do this. For exampe, simpleXML

// Your XML does not inlude root element. 
// If real does, remove `root`from the next line
$xml = simplexml_load_string('<root>' . $text . '</root>'); 
for($i = 0; $i < count($xml->contactData); $i++) {
    unset($xml->contactData[$i]->email);
    unset($xml->contactData[$i]->phone);
}

echo $xml->saveXML();