Wednesday, April 22, 2009

PHP - How to remove a HTML element property value

It's pretty simple but I know regular expressions can be tough sometimes. The code below will remove the width property regardless if it's value is enclosed with single or double quotes.

$str = preg_replace('/width\s*=\s*["\'].*["\']/','',$str);
Let's put it into a function and change width to a variable so that this code is more flexible:

function remove_property($property,$str){
return preg_replace('/'.$property.'\s*=\s*["\'].*["\']/','',$str);
}

$str = '<img src="image.jpg" width="200" height="230">';
$stripped_string = remove_property('width',$str);
Enjoy!

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){
while(stristr($str,'',$start)){
$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="http://www.blogger.com/somefile.jpg" 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.

Ciao