Possible Duplicate:
PHP – How to replace a phrase with another?
I need to transform this piece of html
<html>
<body>
Lorum ipsum <a href="http://google.com">click here</a> dolores lorem.
Lorum ipsum <a href="http://stackoverflow.com">click here too</a> dolores lorem.
</body>
</html>
To:
<html>
<body>
Lorum ipsum @@1@@ dolores lorem.
Lorum ipsum @@2@@ dolores lorem.
</body>
</html>
How can this be achieved using Domdocument (I’m not interested in een regex solution)?
You have not shown any code so far so it is not really clear into which problem you run. I can only assume that it is because if you iterate over the list of links changing it, the iteration will get invalid. So only the first element is being replaced.
Using a
forloop can help here only getting the first a element per each iteration. It would also allow to initialize and increase the count variable for the numbers you need in the replacement.The replacement itself can be easily done with
replaceChild. The loop example:The call to
$doc->getElementsByTagName('a')->item(0)will returnNULLif no such element exists (any longer). That is the exit condition of the loop.Full example:
Output:
I hope this is helpful.