用于在 PHP 中提取图像路径的正则表达式

RegEx for extracting image paths in PHP

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

输入

<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,我想提取图像 URL 上面标签中的任何格式(jpg 或 png):

尝试

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?

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

$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);

所有输出:

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"
  }
}

期望的输出

$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"
}