How to replace the <img> tag/s present in an string with another string that represents some code in PHP? -
i've 1 following string:
$feed_status = nice <img src=\"http://52.1.47.143/file/pic/emoticon/default/smile.png\" alt=\"smile\" title=\"smile\" title=\"v_middle\" /> see <img src=\"http://52.1.47.143/file/pic/emoticon/default/happy.png\" alt=\"smile\" title=\"smile\" title=\"v_middle\" /> again <img src=\"http://52.1.47.143/file/pic/emoticon/default/tongue.png\" alt=\"smile\" title=\"smile\" title=\"v_middle\" />;
i've array titled $emoticon_codes
containing 3 elements needs placed in above string in place of <img>
tags.
array ( [0] => \ue056 [1] => \ue057 [2] => \ue105 )
the 3 <img>
tags string should replaced above 3 strings in same order.
how should achieve in optimum way?
please me. in advance.
my final string should after printing :
nice \ue056 see \ue057 again \ue105;
i tried following thing :
$doc = new domdocument(); $doc->loadhtml($feed_status); $imagetags = $doc->getelementsbytagname('img'); if(count($imagetags)) { $emoticon_codes = array(); foreach($imagetags $tag) { /*echo basename($tag->getattribute('src')); echo "<br>";*/ if (basename($tag->getattribute('src')) == 'evilgrin.png') { array_push($emoticon_codes, '\ue404'); } if (basename($tag->getattribute('src')) == 'grin.png') { array_push($emoticon_codes, '\ue415'); } if (basename($tag->getattribute('src')) == 'happy.png') { array_push($emoticon_codes, '\ue057'); } if (basename($tag->getattribute('src')) == 'smile.png') { array_push($emoticon_codes, '\ue056'); } if (basename($tag->getattribute('src')) == 'surprised.png') { array_push($emoticon_codes, '\ue107'); } if (basename($tag->getattribute('src')) == 'tongue.png') { array_push($emoticon_codes, '\ue105'); } if (basename($tag->getattribute('src')) == 'unhappy.png') { array_push($emoticon_codes, '\ue403'); } if (basename($tag->getattribute('src')) == 'waii.png') { array_push($emoticon_codes, '\ue407'); } if (basename($tag->getattribute('src')) == 'wink.png') { array_push($emoticon_codes, '\ue405'); } } }
not tested, "proper" way it:
$arr = array(\ue056, \ue057, \ue058); $html = 'your html string here'; $i = 0; $dom = new dom(); $dom->loadhtml($html); $images = $dom->getelementsbytagname('img'); foreach($images $img) { $img->parentnode->replacechild($img, $dom->createtextnode($arr[$i])); $i++; if ($i > count($arr)) { break; } }
Comments
Post a Comment