Last active
August 29, 2015 14:16
-
-
Save hatzopoulos/e30f5c38488c3d4fdfa0 to your computer and use it in GitHub Desktop.
preg_replace for lazy-load
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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