javascript - How to edit or replace xml string using php? -


i had change question simple question

for example: xml file name is: abc.xml

inside of xml file is:

<li class="thumb"><a class="alibaba" href="google.com"></a></li> 

so, here question: how use php search string inside of abc.xml , find alibaba name , replace alibabacolor name , save abc.xml

please guide me , give me example this. thank you

you can using php str_replace among many other functions. here couple examples ->

if xml in string can this

php:

<?php     $myxmlstring = str_replace('alibaba', 'alibabacolor', $myxmlstring); ?> 

if need xml data file there few ways to depending on if need save it, replace , display it, or doing. of comes down preference.

just need display it?

<?php     $xml = file_get_contents(/path/to/file); // or http://path.to/file.xml     $myxmlstring = str_replace('alibaba', 'alibabacolor', $xml); ?> 

need change file itself?

<?php     $xml = file_get_contents(/path/to/file); // or http://path.to/file.xml     $myxmlstring = str_replace('alibaba', 'alibabacolor', $xml);     file_put_contents(/path/to/file, $myxmlstring); ?> 

--

to answer below comment, here working code server ->

xml:

<li>hello</li> 

php:

$file = '/home/username/public_html/xml.xml'; $xml = file_get_contents($file); $text = str_replace('hello','world',$xml); file_put_contents($file, $text); 

xml.xml after:

<li>world</li> 

keep in mind change 'username' right one, or use fqdn if more comfortable doing so. make sure file has permission written script well.


Comments