Skip to content

Instantly share code, notes, and snippets.

@lonalore
Created August 16, 2019 13:52
Show Gist options
  • Save lonalore/ae013be31e46e0977a68e2f9b0b36945 to your computer and use it in GitHub Desktop.
Save lonalore/ae013be31e46e0977a68e2f9b0b36945 to your computer and use it in GitHub Desktop.
PHP: get image src attribute (regular expression)
<?php
$html = '<img border="0" src="/images/image1.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image2.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image3.jpg" alt="Image" width="100" height="100" />';
preg_match_all('@src="([^"]+)"@', $html, $match);
$src = array_pop($match);
print_r($src);
// Array (
// [0] => /images/image1.jpg
// [1] => /images/image2.jpg
// [2] => /images/image3.jpg
// )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment