Last active
February 17, 2018 12:33
-
-
Save codingfox-rus/e235575f9b1894283f476d4471d4906a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
$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