Skip to content

Instantly share code, notes, and snippets.

@codingfox-rus
Last active February 17, 2018 12:33
Show Gist options
  • Save codingfox-rus/e235575f9b1894283f476d4471d4906a to your computer and use it in GitHub Desktop.
Save codingfox-rus/e235575f9b1894283f476d4471d4906a to your computer and use it in GitHub Desktop.
<?php
$html = '<div><img alt="" src="http://example.com/" alt="test"></div>';
$imgPattern = '/<img\s(.*)\/?>/U';
preg_match_all($imgPattern, $html, $matches, PREG_SET_ORDER);
$uId = 1;
foreach ($matches as $match) {
$img = trim($match[0]);
$attrContent = trim($match[1]);
preg_match('/src=\"(.*)\"/', $attrContent, $srcMatch);
if (!empty($srcMatch)){
$src = $srcMatch[1];
$imgAttr = "Image-". $uId;
$out = '<a href="'. $src .'" data-lightbox="'. $imgAttr .'">'. $img .'</a>';
$html = str_replace($img, $out, $html);
}
$uId += 1;
}
print_r($html);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment