how to select image src using PHP

£可爱£侵袭症+ 提交于 2020-01-15 18:53:14

问题


So i have some images with several forms like this :

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

and like this :

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

so my code to select the src image is :

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

i tried that several times but the code doesn't worked, so please if someone has any idea i will be very appreciative.


回答1:


Try this way to grab src from image src="([^"]+)"

EDIT : see regex here https://www.regex101.com/r/yF8tJ1/1

CODE EXAMPLE:

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


来源:https://stackoverflow.com/questions/27988667/how-to-select-image-src-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!