Skip to content

Instantly share code, notes, and snippets.

@solymosi
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solymosi/6a7eeae5113e728f2084 to your computer and use it in GitHub Desktop.
Save solymosi/6a7eeae5113e728f2084 to your computer and use it in GitHub Desktop.
Advanced lazy load regex fix
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