I’ve this PHP script function that use to extract image tag attributes. This comes very useful when you want to extract image data from some html content for processing or migration. if you know how to modify the regular expression, then the same function can be use to extract any other html attributes! Just few line of codes and hope it benefits everyone here 🙂
To extract img tag attribute using PHP, follow the steps below
Advertisements
- This is a very straight forward function that you can copy and use to extract img tag attributes instantly in your project.
function extract_attrib($tag) { preg_match_all('/(id|alt|title|src)=("[^"]*")/i', $tag, $matches); $ret = array(); foreach($matches[1] as $i => $v) { $ret[$v] = $matches[2][$i]; } return $ret; } $img_tag = '
';
$atts = extract_attrib($img_tag);
print_r($atts);
/** Result:-
Array
(
[id] => "logo thumbnail"
[src] => "http://www.yourdomain.com/wordpress.jpg"
[alt] => "wordpress logo for thumbnail display"
[title] => "wordpress logo"
)
*/
Happy coding!
Related posts:
Symfony 1.4 - Customize Admin Generator listing to show data from foreign tables
PHP: How to convert ISO character (HTMLEntities) to UTF-8?
How to remove newline character in PHP
WordPress: How to upload image using script?
PHP Fatal error: Class ‘DOMDocument’ not found in …
Speed Up and Save Your Website Bandwidth with GZip Compression
How to downgrade to PHP 5.2 using MacPort
How to install apache, php, mysql with macport in Mac OS X
Share this with your friends:-