<?php // Your code here! $pattern ='/<img.*?alt="News18".*>/'; $finaldata= 'this is dummy string <img loading="lazy" src="http://hindicmsrevamp.news18.com/wp-content/uploads/2022/06/1655804926_1614700942_betel-nut-5.jpg" alt="Bappi Lahiri" width="875" height="580" class="alignnone size-full wp-image-4214823"> this is another sentesnces <img src="imagetwo.jpg" alt="News18"/> this is third one'; $doc = new DOMDocument(); @$doc->loadHTML($finaldata); $tags = $doc->getElementsByTagName('img'); $val['Keyword']='Bappi Lahiri'; $val['link']='#'; foreach ($tags as $tag) { $src=$tag->getAttribute('src'); $alt=$tag->getAttribute('alt'); $tag->setAttribute( 'alt', 'hihihih' ); $a = $doc->createElement('a'); $attrib = $doc->createAttribute( 'href'); } echo $doc->saveHTML(); //echo $finaldata = preg_replace('/(<img [^>]*alt="'.$val['Keyword'].'"[^>]*>)/', '<a href="$2">$1</a>', $finaldata); // echo $finaldata = preg_replace('/(<img [^>]*alt="'.$val['Keyword'].'"[^>]*>)/', '<a href="'.$val['link'].'">$1</a>', $finaldata); ?>
Jagdish Sarma Asked question June 21, 2022