regex to wrap img tag with href containg the src

后端 未结 3 1105
别跟我提以往
别跟我提以往 2021-01-27 12:38

[Edited - Sorry Bart] I\'ve looked at other answers but struggling to match this. I want to wrap an image tag where the src is the second attribute (after title) with a specific

3条回答
  •  盖世英雄少女心
    2021-01-27 13:19

    Try this code:

    ';
    
    preg_match('#src="(?:.*/)?(.*?)"#', $str, $match);
    $src = $match[1];
    ?>
    ” />
    

    EDIT: another version to account for multiple tags in the string:

    $replace = '';
    $str = preg_replace('#<\s*img.*?src="(?:[^"]+/)?(.*?)".*?>#s', $replace, $str);
    

提交回复
热议问题