如何使用PHP选择图像源

2022-03-22 00:00:00 php preg-replace preg-match-all

所以我有一些具有以下几种形式的图像:

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

我试了几次,但代码不起作用,如果有人有任何想法,我将不胜感激。


解决方案

尝试此方式从映像src="([^"]+)"

抓取src

编辑:查看regex此处https://www.regex101.com/r/yF8tJ1/1

代码示例:

$re = "/src="([^"]+)"/"; 
$str = "<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>"; 

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

相关文章