I'd like to replace the value in this tag using PHP's str_replace function:
<address2>Replace this value</address2>
What is the best approach to do this?
I'd like to replace the value in this tag using PHP's str_replace function:
<address2>Replace this value</address2>
What is the best approach to do this?
Try this :
$nodeAdresseValue = str_replace("%value%", "your value", "<address2>%value%</address2>");
Do not use string functions on XML, use DOMDocument instead, it'll help you parse XML more easily, here is an example code DEMO:
<?php
$string = "<address2>Replace this value</address2>";
$domDocument = new DOMDocument();
$domDocument->loadXML($string);
$address2Elements = $domDocument->getElementsByTagName('address2');
foreach ($address2Elements as $address2) {
$address2->nodeValue = "Value Replaced";
}
var_dump($domDocument->saveXML());
Output:
string(58) "<?xml version="1.0"?>
<address2>Value Replaced</address2>
"
$search = "/[^<address2>](.*)[^<\/address2>]/";
$replace = "replace with me";
$string = "<address2>Replace this value </address2>";
echo preg_replace($search,$replace,$string);
this will work irrespective of the text, it will work even if you dont know the value inside tags