-
-
Save solymosi/6a7eeae5113e728f2084 to your computer and use it in GitHub Desktop.
Advanced lazy load regex fix
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
Hi Kason, | |
I found another bug; this time it's with the regex you use to replace the image src attributes. Because the quantifiers are greedy by default, the regex breaks if the string "src" appears anywhere after an image tag. For example: | |
<img src="https://something" /> this is a cool srcipt, and a typo | |
becomes: | |
<img src="https://something" /> this is a cool src="http://.../shade.gif" ImageHoldeript, and a typo | |
The solution is to use the following regex: | |
$pattern = '/((?:\<img).*?)(src)/'; // note the question mark after the star | |
You can make it even more robust by matching on the string " src=" instead of just "src": | |
$pattern = '/((?:\<img).*?)( src=)/'; | |
$plugin_dir_path = plugin_dir_url(__FILE__); | |
$buffer = preg_replace($pattern, "$1 src=\"" . $plugin_dir_path . "shade.gif\" ImageHolder=", $buffer); | |
Please take a look at this and update the plugin as necessary. | |
Thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment