Created
June 11, 2018 03:42
-
-
Save egyjs/922d14d1d27c5b375d0315db04777d0b to your computer and use it in GitHub Desktop.
PHP | get all <img> tag and extract the “src” attribute
This file contains 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 | |
function getImgFromString($html){ | |
preg_match_all('/<img[^>]+>/i',$html, $result); | |
$img = array(); | |
$i = 0; | |
foreach( $result[0] as $img_tag) | |
{ | |
preg_match_all('/(src)="([^"]+)"/i',$img_tag, $img[$i]); | |
$i++; | |
} | |
$arr0 = array(); | |
for ($x0 = 0; $x0 < count($img); $x0++) { | |
for($x1 = 0;$x1 < count($img[$x0][1]); $x1++){ | |
$arr0[$x0][$img[0][1][$x1]] = $img[$x0][2][$x1]; | |
} | |
} | |
return $arr0; | |
} | |
/* --OUTPUT WILL BE LIKE THIS: | |
Array | |
( | |
[0] => Array | |
( | |
[src] => data:image/png;base64,1 | |
) | |
[1] => Array | |
( | |
[src] => https://avatars2.githubusercontent1.com/u/12745270?s=52&v=4 | |
) | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment