开发者

Help with Regex in PHP

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>


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.


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.


Forget regex's, use this instead:

http://simplehtmldom.sourceforge.net/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜