0

I'm a bit lost here. I'm trying to replace src tag with data:image/gif;base64,R0lGODlhAQABAPABAAAAAP///yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw== for lazy loading and add class lazyload so lazysizes could work. But none of that is successful here.

Am I missing something?

public static function filter_lazy_load_gravatar( $html ) {
    $html = preg_replace( '/src="(http:\/\/([^\/]+)\/)?([^"]+)"/', 'src="data:image/gif;base64,R0lGODlhAQABAPABAAAAAP///yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=="', $html);

    $html = str_replace( 'srcset=', 'data-srcset=', $html );
    $pat = '/class="([^"]*)"/';
    $html = preg_replace($pat, 'class="$1 lazyload"', $html);
    return $html;
}

Input

<img alt="" src="http://gravatar.com/image.jpg" srcset="http://0.gravatar.com/avatar/0b11f7eec98d2ee78414a4322ea94a01?s=400&amp;d=mm&amp;r=g 2x" class="avatar avatar-200 photo" height="200" width="200">

Expected output

<img alt="" src="data:image/gif;base64,R0lGODlhAQABAPABAAAAAP///yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" data-srcset="http://0.gravatar.com/avatar/0b11f7eec98d2ee78414a4322ea94a01?s=400&amp;d=mm&amp;r=g 2x" class="avatar avatar-200 photo lazyload" height="200" width="200">
toy
  • 11,711
  • 24
  • 93
  • 176
  • What is `$html`? You probably should use a parser for this, http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php....or which regex fails currently? You don't need second regex actually something like `str_replace('class=", class="lazyload` – chris85 Aug 06 '15 at 19:50
  • Never use regex on html... use a parser... – Random Aug 06 '15 at 19:57
  • I'm doing WordPress development. For some reason when I do it never works $doc = DOMDocument::loadHTML( $html ); – toy Aug 06 '15 at 20:08
  • Expected output is the exact output... https://eval.in/413329 – chris85 Aug 06 '15 at 20:19
  • [Class 'DOMDocument' not found](https://wordpress.org/support/topic/plugin-import-html-pages-requires-php-xml-if-you-get-error-domdocument-not-found?replies=1)? – Wiktor Stribiżew Aug 06 '15 at 21:12

0 Answers0