Defpult
Defpult

Reputation: 11

Get attribute and inner HTML of a specified, custom HTML tag

Let's assume I do preg_replace() as follows:

preg_replace ("/<my_tag>(.*)<\/my_tag>/U", "<my_new_tag>$1</my_new_tag>", $sourse);

That works but I do also want to grab the attribute of the my_tag - how would I do it with this:

<my_tag my_attribute_that_know_the_name_of="some_value">tra-la-la</my_tag>

Upvotes: 1

Views: 87

Answers (3)

Alan Moore
Alan Moore

Reputation: 75272

preg_replace('#<my_tag\b([^>]*)>(.*?)</my_tag>#',
   '<my_new_tag$1>$2</my_new_tag>', $source)

The ([^>]*) captures anything after the tag name and before the closing >. Of course, > is legal inside HTML attribute values, so watch out for that (but I've never seen it in the wild). The \b prevents matches of tag names that happen to start with my_tag, preventing bogus matches like this:

<my_tag_xyz>ooga-booga</my_tag_xyz><my_tag>tra-la-la</my_tag>

But that will still break on <my_tag> elements wrapped in other <my_tag> elements, yielding results like this:

<my_tag><my_tag>tra-la-la</my_tag>

If you know you'll never need to match tags with other tags inside them, you can replace the (.*?) with ([^<>]++).

I get pretty tired of the glib "don't do that" answers too, but as you can see, there are good reasons behind them--I could come up with this many more without having to consult any references. When you ask "How do I do this?" with no background or qualification, we have no idea how much of this you already know.

Upvotes: 1

Jack Kelly
Jack Kelly

Reputation: 18667

You don't use regex. You use a real parser, because this stuff cannot be parsed with regular expressions. You'll never know if you've got all the corner cases quite right and then your regex has turned into a giant bloated monster and you'll wish you'd just taken fredley's advice and used a real parser.

For a humourous take, see this famous post.

Upvotes: 1

fredley
fredley

Reputation: 33941

Forget regex's, use this instead:

http://simplehtmldom.sourceforge.net/

Upvotes: 0

Related Questions