note 112357 added to domelement.setattribute

From: Date: Thu, 06 Jun 2013 09:44:37 +0000
Subject: note 112357 added to domelement.setattribute
Groups: php.notes 
Request: Send a blank email to php-notes+get-195091@lists.php.net to get a copy of this message
The use of Dom to first remove and then add the width and height to the first img tag from the text.I hope it help you to save your time <?php $html = ' <img src="http://www.example.com/images/header.jpg" width="898" height="223" style="border-bottom:5px solid #cccccc;"/> <img src="http://www.example.com/images/header2.jpg" width="898" height="223" style="border-bottom:5px solid #cccccc;"/> '; $doc = DOMDocument::loadHTML($html); $c =0; foreach($doc->getElementsByTagName('img') as $image){ if ($c>0) continue; foreach(array('width', 'height') as $attribute_to_remove){ echo $attribute_to_remove; if($image->hasAttribute($attribute_to_remove)){ $image->removeAttribute($attribute_to_remove); } if($attribute_to_remove=='height'){ if(!$image->hasAttribute($attribute_to_remove)){ $image->setAttribute($attribute_to_remove,'220'); }} if($attribute_to_remove=='width'){ if(!$image->hasAttribute($attribute_to_remove)){ $image->setAttribute($attribute_to_remove,'700'); }} $c = $c+1; } } echo $doc->saveHTML(); ---- Server IP: 69.147.83.199 Probable Submitter: 80.87.31.34 ---- Manual Page -- http://php.net/manual/en/domelement.setattribute.php Edit -- https://master.php.net/note/edit/112357 Del: integrated -- https://master.php.net/note/delete/112357/integrated Del: useless -- https://master.php.net/note/delete/112357/useless Del: bad code -- https://master.php.net/note/delete/112357/bad+code Del: spam -- https://master.php.net/note/delete/112357/spam Del: non-english -- https://master.php.net/note/delete/112357/non-english Del: in docs -- https://master.php.net/note/delete/112357/in+docs Del: other reasons-- https://master.php.net/note/delete/112357 Reject -- https://master.php.net/note/reject/112357 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195091) next »