2016-05-25 10 views
1

Ich muss einige Daten aus einem Protokoll von einigen Xml schrubben und ich muss wissen, wie preg_replace alle Vorkommen einer Regex-Übereinstimmung ersetzen.PHP preg_replace alle Instanz entspricht

Die XML sieht in etwa so aus.

<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>[email protected]</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>[email protected]</email> 
</contactData> 

Wenn ich dies auf diesem XML ausführen.

$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); 

Ich bekomme das.

<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>[email protected]</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> 

Wie bekomme ich es ersetzen die anderen "contactData" E-Mail und Telefon?

+1

haben Sie einen Blick auf diese Frage und Antwort: http://stackoverflow.com/questions/10523887/replace-all-occurrences-inside-pattern –

+2

Verwenden DOMDocument und DOMXPath, nicht Verwenden Sie Regex dafür. –

+0

http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – AbraCadaver

Antwort

2

Es ist richtiger, einen XML-Parser zu verwenden, um dies zu tun. Für 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(); 
+1

Sie haben Recht. Ich habe es vermieden, weil ich die XML-Anfrage so unberührt wie möglich halten wollte, aber es ist albern, dafür Regex zu verwenden, das mit möglichen Problemen behaftet ist. – Halfstop

+2

Ja, Sie haben die richtige Entscheidung getroffen. Viel Glück! – splash58