如何选择图片src使用PHP(how to select image src using PHP)

2019-10-21 15:35发布

所以,我有几种形式类似这样的一些图片:

<a href="" class="link-img" alt="">
  <img editable="true" style="display: block; cursor: default;" class="main-image" 
    width="538" height="auto" src="src" alt="large image">
</a>

而像这样:

<a href="" class="link link-img">
 <img src="src" style="width: 100%; display: block; cursor: pointer;" editable="true" 
  class="main-image imageLink" width="" height="auto" alt="">
</a>

所以我的代码来选择src图像:

$c = preg_replace('/<a href="(.+)" class="link link-img" alt="(.+)"><img src="(.+)"><\/a>/i'
,'<% link url="$1" caption="<img style=max-width:500px; src=$8 >" html="true" %>',$c);

我试过几次,但代码不工作,所以请,如果有人有任何想法,我将非常感激。

Answer 1:

尝试这种方式抓住src从图像src="([^"]+)"

编辑:见正则表达式这里https://www.regex101.com/r/yF8tJ1/1

代码示例:

$re = "/src=\"([^\"]+)\"/"; 
$str = "<a href=\"\" class=\"link-img\" alt=\"\">\n  <img editable=\"true\" style=\"display: block; cursor: default;\" class=\"main-image\" \n    width=\"538\" height=\"auto\" src=\"src\" alt=\"large image\">\n</a>\n\n<a href=\"\" class=\"link link-img\">\n <img src=\"src\" style=\"width: 100%; display: block; cursor: pointer;\" editable=\"true\" \n  class=\"main-image imageLink\" width=\"\" height=\"auto\" alt=\"\">\n</a>"; 

preg_match_all($re, $str, $matches);


文章来源: how to select image src using PHP