Use preg_replace with condition

0

I have the following string, which can be the representation of an HTML or XML, it makes no difference:

<node1>
    <node2>Conteudo exemplo</node2>
    <node3>Conteudo exemplo</node3>
    <node4>Conteudo exemplo</node4>
    <node5>
        <node6>Conteudo exemplo</node6>
    </node5>
</node1>

I need to create a pattern for preg_replace to replace > matches / strong>, that is, only the > characters representing the closing of a childless child tag in the above example will be overwritten.

I'm aware that there are better ways to manipulate documents with this structure, for example using the DOMDocument Class, but in this case I really need to use preg_replace .

Simulating an expected result by replacing the > character with @ :

<node1>
    <node2@Conteudo exemplo</node2>
    <node3@Conteudo exemplo</node3>
    <node4@Conteudo exemplo</node4>
    <node5>
        <node6@Conteudo exemplo</node6>
    </node5>
</node1>
    
asked by anonymous 28.09.2017 / 15:40

1 answer

2

I do not know if it's the best solution, but try to do so in PHP:

preg_replace('/>(.*?)</', '@$1<', $XML_HTML);
    
28.09.2017 / 16:11