How to change only a part within xml using php?

Asked

Viewed 1,053 times

1

I’m having a little trouble coming to that conclusion. I’ll exemplify, I have the fictional xml below:

<url>http://www.example.com/TITULO<url/>

I just want to change the title part, in case it stays that way:

<url>http://www.example.com/OUTROTITULO<url/>

Any idea how to make this change. I don’t want to change everything, just the part highlighted above.

  • 7

    Depends on a lot of things you didn’t report. Here are some (of the several) that make a difference: Is it always the same tag that the information will be exchanged? Is it just an exchange, or are there several? Is it always the same original string, or can it change? Look for as many details as possible, and preferably as close to the reality of the case. I suggest a read on [Ask] and [tour] for better use of the site.

  • 2

    And yet, it’s actually a string, or this XML has already gone through a parser?

  • a very fast and thick way is to use functions like strpos and/or explode and substr()... Another way is through regular expression, but partifularmente I prefer not to use ER for something very simple.

  • There being no explicit restrictions, and not being necessary to interpret the structure of the XML file, I would use only the function preg_replace

1 answer

1

There are two classes in PHP that will allow you to do this operation.

SimpleXMLElement and DomDocument

I think, with the explanation of the PHP manual, there is no need to post examples.

Basic Simplexml Usage

Another tip is that if you are trying to open the XML of an existing file, use the function simplexml_load_file();

  • +1; using regular expressions to process XML (or HTML, or SQL) is crazy.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.