简体   繁体   中英

PHP preg_match_all multiple HTML tags

I'm looking to use a single regular expression to find 2 multiple kinds of tags. I found in another question regarding this topic that use the | to make a (p|h1|table) looking syntax, it just didn't seem to work for me.

My original pattern for only finding iframes

$removeiframe = preg_replace("#<iframe[^>]+>.*?</iframe>#is", "", $descriptiontext);

The pattern i tried using:

$removeiframe = preg_replace("#<(iframe|img)[^>]+>.*?</(iframe|img)>#is", "", $descriptiontext);

The response I'm currently getting with the last pattern:

If the text contains a single iframe, that gets returned.

If the text contains multiple iframes, it returns this

[
    [0] => '<iframe></iframe>'
    [1] => '<img></img><iframe></iframe>'
]

and what i am trying to get

[
    [0] => '<iframe></iframe>',
    [1] => '<img></img>',
    [2] => '<iframe></iframe>',
]

你应该使用Ungreedy表达式(添加U标志)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM