Skip to content

Instantly share code, notes, and snippets.

@Zauberfisch
Created December 30, 2011 12:30
Show Gist options
  • Save Zauberfisch/1539635 to your computer and use it in GitHub Desktop.
Save Zauberfisch/1539635 to your computer and use it in GitHub Desktop.
[Silverstripe] add <a href="big.png"> to all <img class"popup" src="small.png"> to allow opening full image
class Page extends SiteTree {
...
function Content() {
$content = $this->Content;
// move class to the beginning right after <img
$pattern[0] = '/(<img)([^>]*?)(class=(\'|")([^>]*?)(\'|"))([^>]*?>)/i';
$replacement[0] = '$1 $3$2$7';
// add <a> to all images with class popup and a src having _resampled
$pattern[1] = '/(<img[^>]*?class=("|\')[^>]*?popup[^>]*?("|\')[^>]*?src=("|\')([^>]*?)_resampled\/[^\->]*?-([^>]*?)("|\')[^>]*?>)/i';
$replacement[1] = '<a href="$4$6">$1</a>';
return preg_replace($pattern, $replacement, $content);
}
...
@Zauberfisch
Copy link
Author

NOTE: one might want to test it before using it (:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment