开发者

Regular expression in PHP to return array with all images from html, eg: all src="images/header.jpg" instances

开发者 https://www.devze.com 2023-02-26 20:01 出处:网络
I\'d 开发者_如何学Golike to be able to return an array with a list of all images (src=\"\" values) from html

I'd 开发者_如何学Golike to be able to return an array with a list of all images (src="" values) from html

[0] = "images/header.jpg" [1] = "images/person.jpg"

is there a regular expression that can do this?

Many thanks in advance!


Welcome to the world of the millionth "how to exactract these values using regex" question ;-) I suggest to use the search tool before seeking an answer -- here is just a handful of topics that provide code to do exactly what you need;

  • replacing all image src tags in HTML text
  • getting image src in php
  • How to extract img src, title and alt from html using php?
  • Matching SRC attribute of IMG tag using preg_match
  • php regex : get src value
  • Dynamically replace the “src” attributes of all <img> tags (redux)
  • preg_match_all , get all img tag that include a string


/src="([^"]+)"/

The image will be in group 1.

Example:

preg_match_all('/src="([^"]+)"/', '<img src="lol"><img src="wat">', $arr, PREG_PATTERN_ORDER);

Returns:

Array
(
    [0] => Array
        (
            [0] => src="lol"
            [1] => src="wat"
        )

    [1] => Array
        (
            [0] => lol
            [1] => wat
        )

)


Here is a more polished version of the regular expression provided by Håvard:

/(?<=src=")[^"]+(?=")/

This expression uses Lookahead & Lookbehind Assertions to get only what you want.

$str = '<img src="/img/001.jpg"><img src="/img/002.jpg">';

preg_match_all('/(?<=src=")[^"]+(?=")/', $str, $srcs, PREG_PATTERN_ORDER);

print_r($srcs);

The output will look like the following:

Array
(
    [0] => Array
        (
            [0] => /img/001.jpg
            [1] => /img/002.jpg
        )

)


I see that many peoples struggle with Håvard's post and <script> issue. Here is same solution on more strict way:

<img.*?src="([^"]+)".*?>

Example:

preg_match_all('/<img.*?src="([^"]+)".*?>/', '<img src="lol"><img src="wat">', $arr, PREG_PATTERN_ORDER);

Returns:

Array
(
    [1] => Array
        (
            [0] => "lol"
            [1] => "wat"
        )

)

This will avoid other tags to be matched. HERE is example.

0

精彩评论

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