Skip to content

Instantly share code, notes, and snippets.

@hatzopoulos
Last active August 29, 2015 14:16
Show Gist options
  • Save hatzopoulos/e30f5c38488c3d4fdfa0 to your computer and use it in GitHub Desktop.
Save hatzopoulos/e30f5c38488c3d4fdfa0 to your computer and use it in GitHub Desktop.
preg_replace for lazy-load
<?php
$content = '<img data-should-not-get-stripped src="https://example.com/example.jpg" alt="Not a real image." width="630" height="420">';
echo $content.PHP_EOL.str_repeat('-', 40).PHP_EOL;
$content = preg_replace(
"/<img(?! (data-src|data-img-src|data-orig))([^']*?) src=\"([^']*?)\"([^']*?)>/",
'<img$2$1 data-img-src="$3" src="/r/1.2.3/t.gif'.'"$4>',
$content
);
echo $content.PHP_EOL.str_repeat('-', 40).PHP_EOL;
echo PHP_EOL.'data-should-not-get-stripped found?'.PHP_EOL;
echo strpos($content, 'data-should-not-get-stripped') !== false ? 'yes' : 'no';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment