Wednesday, April 22, 2009

PHP - extracting tag values from HTML elements

Hey all!

I needed to grab all images out of a text string and then get image property values from that string. I could not use getimagesize() because first I needed the value of the src property anyway.

function gather_images($str){
$images[] = $cur = substr($str,$start,$end);
$str = str_replace($cur,"",$str);
return array($str,$images);

So now I've got my image strings in the images[] array. An image string could look something like this <img src="" width="100" height="130" />. The next part is to get a property such as width out of the image string:

function get_tag_info($value,$str){
if(preg_match('/'.$value.'\s*=\s*".*"/',$str)){ // if enclosed with " "
$value = get_tag_value($value,$str,'"');
else if(preg_match('/'.$value."\s*=\s*'.*'/",$str)){ // if enclosed with ' '
$value = get_tag_value($value,$str,"'");
return $value;
function get_tag_value($value,$str,$quote){
$vpos = strpos($str,$value);
$q1 = strpos($str,$quote,$vpos);
$q2 = strpos($str,$quote,$q1+1);
return substr($str,$q1+1,$q2-$q1-1);

So there you have it, add customization and error handling as you like.


No comments:

Post a Comment