0

Possible Duplicate:
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....

2
  • This question isn't very specific, but from what you've said it looks like you're trying to parse an XML document or something similar. In that case you'll probably want to investigate the simpleXML extensions. Commented Aug 12, 2011 at 9:23
  • Yes Iam parsing XML, but using ModX CMS and ditto snippet with phx Commented Aug 12, 2011 at 10:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.