Created
January 3, 2020 02:54
-
-
Save pentagonal/4bd3af403bfe79f5c23660ca679ca25e to your computer and use it in GitHub Desktop.
gawe wakidi
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 | |
// sample HTML | |
$url = 'https://www.shunt-magetan.org/wp-content/uploads/wordpress-popular-posts/1904-featured-75x75.jpg?test=1'; | |
$url2 = 'https://www.shunt-magetan.org/wp-content/uploads/wordpress-popular-posts/1904-featured-2-75x75.jpg?test=2'; | |
$html = <<<HTML | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf8"> | |
<title>SAMPLE</title> | |
</head> | |
<body> | |
<div class="wrap"> | |
<img src="{$url}" width="115" height="115"> | |
<img src="{$url2}" width="115" height="115"> | |
</div> | |
</body> | |
</html> | |
HTML; | |
/** | |
* Split Image As Aray | |
* @param string $url url to split | |
* @return array|bool false if failed, array if exists image | |
*/ | |
function image_split(string $url) | |
{ | |
preg_match( | |
'~^ # START WITH | |
(?P<url> # CATCH FULL URL | |
(?:(?P<url_protocol>https?)\:)(?:\/\/) # GET THE PROTOCOL HTTP / HTTPS | |
(?P<url_domain>[^\/]+) # GETTING DOMAIN | |
(?P<url_path> # GETTING FULL URL PATJ | |
(?P<img_prefix_path>\/.+) # GETTING URL PREFIX PATH ON IMAGE SIZE ONLY | |
\/ # IGNORE PATH | |
(?P<img_file_name> # GETTING IMAGE NAME | |
(?P<img_prefix_size>[^\/]+) # GETTING URL PREFIX IMAGE | |
(?: | |
\- | |
(?P<img_size> # GETTING IMAGE SIZE | |
(?P<img_width>[0-9]+) # GETTING IMAGE HIGH | |
(?:[x]) # (x) SIGN MEAN SIZE SEPARATOR | |
(?P<img_height>[0-9]+) # GETTING IMAGE img_height | |
) | |
) # ADD ? IF THERE ARE SIZE OR NOT | |
(?:\.(?P<img_extension>[a-z]+))? # IF NO img_extension it maybe has a dynamic image | |
) | |
) | |
) | |
# GETTING QUERY IF EXISTS | |
(?: | |
\? | |
(?P<query_string>.+) | |
)? | |
$ # END REGEX | |
~xi', | |
$url, | |
$match | |
); | |
$default = [ | |
'url' => '', | |
'url_protocol' => '', | |
'url_domain' => '', | |
'url_path' => '', | |
'img_prefix_path' => '', | |
'img_file_name' => '', | |
'img_prefix_size' => '', | |
'img_size_prefix' => '', | |
'img_size' => '', | |
'img_width' => null, | |
'img_height' => null, | |
'img_extension' => '', | |
'query_string' => '', | |
'query' => [], | |
]; | |
$match = array_filter($match, 'is_string', ARRAY_FILTER_USE_KEY); | |
if (!empty($match)) { | |
$match = array_merge($default, $match); | |
parse_str($match['query_string'], $match['query']); | |
return $match; | |
} | |
return false; | |
} | |
$images = []; | |
$html = preg_replace_callback('~<(body)[^>]*>(?P<body_content>.*)<\/\1>~is', function ($match) use (&$images) { | |
if (!isset($match['body_content']) || empty($match['body_content'])) { | |
return $match[0]; | |
} | |
return preg_replace_callback( | |
'~(?P<prefix><img.+src=([\'\"]))(?P<img>[^\'\"]+)(?P<sufix>[^>]*>)~i', | |
function ($im) use (&$images) { | |
$data = image_split($im['img']); | |
if (is_array($data)) { | |
$images[$im['img']] = $data; | |
// replace here | |
/* | |
*/ | |
} | |
// return $im['prefix'].preg_replace('~\?.+$~', '', $im['img']).$im['sufix']; // example replace query | |
return $im['prefix'].$im['img'].$im['sufix']; | |
}, | |
$match['body_content'] | |
); | |
}, $html); | |
echo $html; | |
print_r($images); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment