0

This is the HTML code:

<div class="comment-content" contenteditable="true">
     Hello all<br/>
    <img alt="" class="emojioneemoji" src="https://cdnjs.cloudflare.com/ajax/libs/emojione/2.2.7/assets/png/1f602.png">
    <img alt="" class="emojioneemoji" src="https://cdnjs.cloudflare.com/ajax/libs/emojione/2.2.7/assets/png/1f602.png">
    <img alt="" class="emojioneemoji" src="https://cdnjs.cloudflare.com/ajax/libs/emojione/2.2.7/assets/png/1f602.png">
</div>

I want a PHP code to search content if it images and replaces the images with their alt attribute.

I want the output to be:

<div class="comment-content" contenteditable="true">
     Hello all<br/>
    
    
    
</div>

I try to make this by using Regex and this is my code but it's not working:

preg_replace('/<img.*?alt="(.*?)"[^\>]+>/g', '$1', $comment_content)

Any guide really appreciate.

Thank you.

John
  • 171
  • 10
  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – Spoody Apr 07 '18 at 22:45
  • @MehdiBounya I am sorry i edited the question I hope you can help me. – John Apr 07 '18 at 22:52
  • 1
    https://stackoverflow.com/a/1732454/522479, https://secure.php.net/manual/en/class.domdocument.php – Cobra_Fast Apr 07 '18 at 22:54
  • It worked for me by removing `g` from the regex, but consider @Cobra_Fast comment – Spoody Apr 07 '18 at 22:56
  • @MehdiBounya Yes, I forget to remove g sorry. – John Apr 07 '18 at 23:01
  • @Cobra_Fast I want to ask you a question to understand more I know that using DOMDocument is better but using Regex in my case is something bad? if yes why? – John Apr 07 '18 at 23:09

1 Answers1

1
<?php
$html = '<div class="comment-content" contenteditable="true">
     Hello all<br/>
    <img alt="" class="emojioneemoji" src="https://cdnjs.cloudflare.com/ajax/libs/emojione/2.2.7/assets/png/1f602.png">
    <img alt="" class="emojioneemoji" src="https://cdnjs.cloudflare.com/ajax/libs/emojione/2.2.7/assets/png/1f602.png">
    <img alt="" class="emojioneemoji" src="https://cdnjs.cloudflare.com/ajax/libs/emojione/2.2.7/assets/png/1f602.png">
</div>';
$dom = new DOMDocument();
$dom->loadHTML('<html><head><meta charset="utf-8"></head><body>' . $html . '</body></html>');
while ($dom->getElementsByTagName('img')->length) {
    $oldnode = $dom->getElementsByTagName('img')[0];
    $newnode = $dom->createTextNode($oldnode->getAttribute('alt'));
    $oldnode->parentNode->replaceChild($newnode, $oldnode);
}

header('Content-Type: text/html; charset= UTF-8');
echo $dom->saveHTML($dom->getElementsByTagName('div')[0]);
Ermac
  • 1,181
  • 1
  • 8
  • 12
  • Thank you but why not using $dom->saveHTML() or $dom->saveHTML($dom->documentElement)) directly? – John Apr 07 '18 at 23:16
  • Hi, I used it to show only the contents of the `div` element, if I used `$dom->saveHTML()` it will show the `div`'s contents wrapped in the code that I added when I loaded the HTML and some other code added by the class itself. – Ermac Apr 07 '18 at 23:27
  • Also I wrapped your HTML code with `` etc code so the class will treat the HTML in UTF-8 charset, it is the solution I have found by searching Stackoverflow or else as they said the HTML will be treated in ISO-8859-1 charset. – Ermac Apr 07 '18 at 23:30