1

I get html content from ckeditor and i have following html content

<p><strong>Hello</strong></p>
<p><img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAMACAIAA" style="height:131px; width:234px" /></p>
<p><strong>How are you</strong></p>
<p><img alt="" src="data:image/png;base64,ABAXKuVAAAAA3NCSVQICAjb4U/gAAAgAElEQV" style="height:142px; width:253px" /></p>
<p><strong>Good Morning</strong></p>
<p>&nbsp;</p>

Now i want to replace src of each image tag with different image name. Let says for example

<p><strong>Hello</strong></p>
<p><img alt="" src="img.jpg" style="height:131px; width:234px" /></p>
<p><strong>How are you</strong></p>
<p><img alt="" src="test.jpg" style="height:142px; width:253px" /></p>
<p><strong>Good Morning</strong></p>
<p>&nbsp;</p>

source of image is dynamically bind. so it can be anything.after this replacement i save this html content to database.

i have done with

$image_name ='<p><strong>Hello</strong></p>
    <p><img alt="" src="img.jpg" style="height:131px; width:234px" /></p>
    <p><strong>How are you</strong></p>
    <p><img alt="" src="test.jpg" style="height:142px; width:253px" /></p>
    <p><strong>Good Morning</strong></p>
    <p>&nbsp;</p>';
    $html = preg_replace('!(?<=src\=\").+(?=\"(\s|\/\>))!', 'img.jpg',$image_name );

But this replace all src same.

I want whole content same except src of <img> tag. I need to replace this content using regex. I prefer regex because i want to save this replaced html into database. If any other solution works then that is also good.

  • _ONLY REGEX_ ! Why do you hate **DOM** methods ? –  Jan 26 '16 at 06:34
  • Is there any reason why you dont want to use a DOM parser? – Charlotte Dunois Jan 26 '16 at 06:38
  • What's the specific question? This isn't a code factory ... where you make an order and it gets created and shipped to you. Show what you tried to solve this with and ran into problems – charlietfl Jan 26 '16 at 06:40
  • Because i want to save this html into db. that's why i want to use REGEX. I with DOM element i can achieve this i will be thankful. –  Jan 26 '16 at 06:42
  • Can you show the PHP code from the output of your html content? – Aljay Jan 26 '16 at 06:42
  • You should be able do this with a parser pretty easily, and more reliably. Take a look at this answer, http://stackoverflow.com/questions/32928184/get-all-images-and-return-the-src/32928222#32928222. – chris85 Jan 26 '16 at 06:51
  • create function that will replace the current src. Take a look at this page for the answer, [link](http://stackoverflow.com/questions/10658348/regex-to-replace-html-src-attribute-in-php) – Aljay Jan 26 '16 at 06:55
  • I have done This with DOM parser. Thanks. but is this good practice to use DOM parser for modify html and save it into db? basically i get this html from ckeditor and wants to replace image src, then save it to db. –  Jan 26 '16 at 07:22
  • Yes, using a parser is good practice, that is what it is made for. – chris85 Jan 26 '16 at 13:23

1 Answers1

4

I have done using DOM parser as suggestion given in comments. Following is my so

$image_name = '<p><strong>Hello</strong></p>
<p><img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAMACAIAA" style="height:131px; width:234px" /></p>
<p><strong>How are you</strong></p>
<p><img alt="" src="data:image/png;base64,ABAXKuVAAAAA3NCSVQICAjb4U/gAAAgAElEQV" style="height:142px; width:253px" /></p>
<p><strong>Good Morning</strong></p>
<p>&nbsp;</p>';

$doc = new DOMDocument();
$doc->loadHTML($image_name);
$img_tags = $doc->getElementsByTagName('img');

$i=0;
foreach ($img_tags as $t )
{
    $savepath = 'img_'.$i.'jpg';
    $t->setAttribute('src',$savepath);
    $i++;
}
$cont = $doc->saveHTML();

This gives me proper result.