PHP: lookup for string in tags and embed it to another tag [duplicate]
Possible Dupl开发者_如何学Cicate:
A simple program to CRUD node and node values of xml file
I have a string (for example $output) with html code containing multiple tags and text. I need to lookup for all ID tags:
<id>123456</id>
and change them to:
<id>123456</id><url>www.webpage.com/somepage.php?id=123456</url>
As you can see the new tag is created and added after ID tag.
Webpage url address is stored in string $url_adr. ID number is always different in each ID tag. And there is multiple ID tags in string, so it needs to be changed one by one I need this code in PHP. Thanks a lot....In simple cases like this, you can do it with a regular expression replace:
$output = '<id>123456</id>';
$url_adr = 'www.webpage.com/somepage.php?id=';
preg_replace('~<id>(.*?)</id>~i', '<id>$1</id><url>'.$url_adr.'$1</url>', $output);
But if you need anything more complicated or full featured, you'd better take a look at proper XML parsers.
精彩评论