linhuiming
因上努力,果上随缘

PHP正则匹配img任意属性src 适用任意img写法

  • LinHuiMing
function replace_content($title,$newstext){
    $newstext = stripcslashes($newstext);
    $imgRegex='/<\s*img[\s\S]+?(?:src=[\'"]([\S\s]*?)[\'"]\s*|alt=[\'"]([\S\s]*?)[\'"]\s*|[a-z]+=[\'"][\S\s]*?[\'"]\s*)+[\s\S]*?>/i';
    $str = preg_replace($imgRegex,'<img class="lazy" data-src="$1" alt="$2" title="$2" width="100%" alt="$2" style="margin:8px 0">',$newstext); 
    return $str;
}
  • YES
  • NO
更多>>

相关文章