RegEx用于在PHP中提取图像路径

问题描述 投票:1回答:1

我想从PHP中给定的img标记中提取特定的模式匹配URL。

Input

<img class="ezlazyload aligncenter size-large wp-image-473313" src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22459%22%20height%3D%22600%22%3E%3C%2Fsvg%3E" alt="shiny-ash-hat-pikachu" width="459" height="600" data-ezsrc="https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.png" data-ezsrcset="https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.png 459w, https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-138x180.png 138w, https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-314x410.png 314w, https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu.png 640w">

上面是我的$img_tag,我想在上面的标签中以任何格式(jpg或png)提取图像URL:

Attempt

preg_match_all('/"(https:\/\/cdn.gamerant.com\/wp-content\/uploads\/.+?\.png)"/s', $img_tag, $matches1, PREG_PATTERN_ORDER);

只有一个提取.png,如果我改为.jpg意味着它提取jpg,如何提取png或jpg,如果存在任何给定的$img_tag

php preg-match-all
1个回答
2
投票

我确信有更简单的方法可以解决您的问题。这只是继续你的原始方法,使用括号()分组:

$img_tag = '<img class="ezlazyload aligncenter size-large wp-image-473313" src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22459%22%20height%3D%22600%22%3E%3C%2Fsvg%3E" alt="shiny-ash-hat-pikachu" width="459" height="600" data-ezsrc="https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.png" data-ezsrcset="https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.PNG 459w, https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-138x180.jpg 138w, https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-314x410.JPEG 314w, https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu.jpeg 640w">';

preg_match_all('/(https:\/\/cdn.gamerant.com\/wp-content\/uploads\/)(.+?\.)(png|jpg|jpeg|JPEG|PNG|JPG)/s', $img_tag, $matches1, PREG_PATTERN_ORDER);

var_dump($matches1);

All Outputs:

array(4) {
  [0]=>
  array(5) {
    [0]=>
    string(77) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.png"
    [1]=>
    string(77) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.PNG"
    [2]=>
    string(77) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-138x180.jpg"
    [3]=>
    string(78) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-314x410.JPEG"
    [4]=>
    string(70) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu.jpeg"
  }
  [1]=>
  array(5) {
    [0]=>
    string(44) "https://cdn.gamerant.com/wp-content/uploads/"
    [1]=>
    string(44) "https://cdn.gamerant.com/wp-content/uploads/"
    [2]=>
    string(44) "https://cdn.gamerant.com/wp-content/uploads/"
    [3]=>
    string(44) "https://cdn.gamerant.com/wp-content/uploads/"
    [4]=>
    string(44) "https://cdn.gamerant.com/wp-content/uploads/"
  }
  [2]=>
  array(5) {
    [0]=>
    string(30) "shiny-ash-hat-pikachu-459x600."
    [1]=>
    string(30) "shiny-ash-hat-pikachu-459x600."
    [2]=>
    string(30) "shiny-ash-hat-pikachu-138x180."
    [3]=>
    string(30) "shiny-ash-hat-pikachu-314x410."
    [4]=>
    string(22) "shiny-ash-hat-pikachu."
  }
  [3]=>
  array(5) {
    [0]=>
    string(3) "png"
    [1]=>
    string(3) "PNG"
    [2]=>
    string(3) "jpg"
    [3]=>
    string(4) "JPEG"
    [4]=>
    string(4) "jpeg"
  }
}

Desired output

$img_tag = '<img class="ezlazyload aligncenter size-large wp-image-473313" src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22459%22%20height%3D%22600%22%3E%3C%2Fsvg%3E" alt="shiny-ash-hat-pikachu" width="459" height="600" data-ezsrc="https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.png" data-ezsrcset="https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.PNG 459w, https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-138x180.jpg 138w, https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-314x410.JPEG 314w, https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu.jpeg 640w">';

preg_match_all('/(https:\/\/cdn.gamerant.com\/wp-content\/uploads\/)(.+?\.)(png|jpg|jpeg|JPEG|PNG|JPG)/s', $img_tag, $matches1, PREG_PATTERN_ORDER);

var_dump($matches1[0]);

对于所需的输出,你可能只是var_dump($matches1[0]);

array(5) {
  [0]=>
  string(77) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.png"
  [1]=>
  string(77) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-459x600.PNG"
  [2]=>
  string(77) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-138x180.jpg"
  [3]=>
  string(78) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu-314x410.JPEG"
  [4]=>
  string(70) "https://cdn.gamerant.com/wp-content/uploads/shiny-ash-hat-pikachu.jpeg"
}
© www.soinside.com 2019 - 2024. All rights reserved.