Skip to content

Instantly share code, notes, and snippets.

@dhruva81
Forked from vyspiansky/image-src-regexpr.php
Created December 26, 2020 08:51
Show Gist options
  • Save dhruva81/ec7dddb9fa7ced9a705d2337d70a0c63 to your computer and use it in GitHub Desktop.
Save dhruva81/ec7dddb9fa7ced9a705d2337d70a0c63 to your computer and use it in GitHub Desktop.
PHP: get image src attribute (regular expression)
<?php
// Source: http://goo.gl/qyLFbg
$html = '<img border="0" src="/images/image.jpg" alt="Image" width="100" height="100" />';
preg_match( '@src="([^"]+)"@' , $html, $match );
$src = array_pop($match);
// will return /images/image.jpg
echo $src;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment