Regular expression to find all “src” attribute of HTML “img” element only folder in PHP

前端 未结 2 1299
不知归路
不知归路 2021-01-29 07:53

I have a string, inside of that I have an image:

\"

balbalba

2条回答
  •  猫巷女王i
    2021-01-29 08:17

    You could do this with a parser and a simple regex to check the attribute starts with required directory...

    $string = '

    balbalba

    balbalba

    balbalba

    '; $doc = new DOMDocument(); $doc->loadHTML($string); $images = $doc->getElementsByTagName('img'); foreach ($images as $image) { if(preg_match('~^img/programacao/~', $image->getAttribute('src'))) { echo $image->getAttribute('src') . "\n"; } }

    Output:

    img/programacao/51.jpg
    img/programacao/46.jpg
    

提交回复
热议问题