php - Replace all images in HTML with text -


i trying replace images in html meet specific requirements appropriate text. specific requirements of class "replaceme" , image src filename in $myarray. upon searching solutions, appears sort of php dom technique appropriate, however, new this. instance, given $html, wish return $desired_html. @ bottom of post attempted implementation doesn't work. thank you

$myarray=array(     'goodimgage1'=>'replacement image 1',     'goodimgage2'=>'replacement image 2' );  $html = '<div> <p>random text , <img src="goodimgage1.png" alt="" class="replaceme">.  more random text.</p> <p>random text , <img src="goodimgage2.png" alt="" class="replaceme">.  more random text.</p> <p>random text , <img src="goodimgage2.png" alt="" class="dontreplaceme">.  more random text.</p> <p>random text , <img src="badimgage1.png"  alt="" class="replaceme">.  more random text.</p> </div>';  $desiredhtml = '<div> <p>random text , replacement image 1.  more random text.</p> <p>random text , replacement image 2.  more random text.</p> <p>random text , <img src="goodimgage2.png" alt="" class="dontreplaceme">.  more random text.</p> <p>random text , <img src="badimgage1.png"  alt="" class="replaceme">.  more random text.</p> </div>'; 

below attempting do..

libxml_use_internal_errors(true);   //temorarily disable errors resulting improperly formed html $doc = new domdocument(); $doc->loadhtml($html);  //what me? $imgs= $doc->getelementsbytagname('img'); foreach ($imgs $img){}  $xpath = new domxpath($doc); foreach( $xpath->query( '//img') $img) {     if(true){   //how check class , image name?         $new = $doc->createtextnode("new attribute");          $img->parentnode->replacechild($new,$img);     } }  $html=$doc->savehtml(); libxml_use_internal_errors(false); 

do this, on way:

$myarray=array(     'goodimgage1.png'=>'replacement image 1',     'goodimgage2.png'=>'replacement image 2' );  $html = '<div> <p>random text , <img src="goodimgage1.png" alt="" class="replaceme">.  more random text.</p> <p>random text , <img src="goodimgage2.png" alt="" class="replaceme">.  more random text.</p> <p>random text , <img src="goodimgage2.png" alt="" class="dontreplaceme">.  more random text.</p> <p>random text , <img src="badimgage1.png"  alt="" class="replaceme">.  more random text.</p> </div>';  $classestoreplace = array('replaceme');  libxml_use_internal_errors(true);   //temorarily disable errors resulting improperly formed html $doc = new domdocument(); $doc->loadhtml($html);  $xpath = new domxpath($doc); foreach( $xpath->query( '//img') $img) {     // classes array     $classes = explode(' ', $img->getattribute('class')); // contain classes assigned element     $classmatches = array_intersect($classes, $classestoreplace);      // preprocess image name match $myarray keys     $imagename = $img->getattribute('src');      if (isset($myarray[$imagename]) && $classmatches) {            $new = $doc->createtextnode($myarray[$imagename]);          $img->parentnode->replacechild($new,$img);     } }  echo var_dump($html = $doc->savehtml()); 

please note following:

  • i made code check images have replaceme class, potentially in addition other classes
  • i added full image file names $myarray keys, simplicity.

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -