开发者

REGEX to find and replace IMG elements [duplicate]

开发者 https://www.devze.com 2023-01-22 17:55 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: I'm looking for a regular expression to remove a given (x)HTML tag from a string
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

I'm looking for a regular expression to remove a given (x)HTML tag from a string

I have a long HTML file and I need to remove all the <img /> tags inside it and all the <a><img /></a> anchors.

What I'm thinking of is writing a PHP script that does the job. But each image and link has different number attributes so I don't know how I can do this neatly. Any help would be 开发者_JAVA技巧strongly appreciated.


Thanks for the answers guys, this is the final solution that I was looking for.

$text = preg_replace("/<a[^>]+\><img[^>]+\><\/a>\n/i", "", $text);

$text = preg_replace("/<img[^>]+\>/i", "", $text);


Use a DOM parser such as PHP Simple HTML DOM Parser or PHP DOM


try <img[^>]+/>

0

精彩评论

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

关注公众号