2

I have multiple img url like this this :-

<img alt="" border="0" src="http://asia.cnet.com/05/i/g/red_arrow_dn.gif">

And I want to replace every start up and close img tag like this in php:-

<div class="miricle_image"><img alt="" border="0" src="http://asia.cnet.com/05/i/g/red_arrow_dn.gif"/></div>

But I am unable to replace start up and close img tag through regex or preg_replace in php.

All img tag created by fckeditor and save in data base and my work is get all img tag through database and replace with

<div class=""><img src=""/></div> 

and save again in database.

Kunwar Siddharth Singh
  • 1,676
  • 6
  • 33
  • 66
  • 1. When the first line of HTML is processed from your browser, all PHP operations have long finished on the server. Try jQuery or other Javascript based libraries for DOM manipulation. 2. See here: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Dave Cartwright Aug 12 '14 at 05:35
  • Thanks for comment @DaveCartwright First Url created by fckeditor and save in data base and my work is get all img tag through database and replace with
    and save again in database.
    – Kunwar Siddharth Singh Aug 12 '14 at 05:40
  • Then you'll need to do a mysql select for all the rows containing this information. Iterate through each of them, replace the wrong information and afterwards update your db accordingly with the new information. Without more specific information I can't provide you with much more advice. – Dave Cartwright Aug 12 '14 at 05:46
  • @DaveCartwright Thanks for comment. I have done everything like get all data through database and etc...but My problem replace img tag. Not a get data through data base or update database information. – Kunwar Siddharth Singh Aug 12 '14 at 05:49
  • For better understanding and learning regex [http://stackoverflow.com/questions/20070505/easy-regular-expression-tutorial-to-learn-it-fast-and-use-it/20070568#20070568][1] [1]: http://stackoverflow.com/questions/20070505/easy-regular-expression-tutorial-to-learn-it-fast-and-use-it/20070568#20070568 – Veerendra Aug 12 '14 at 07:03

4 Answers4

1

Regular expression is not reliable in case of html parsing. It is better to use some html parser in php. simple html dom is a good parser, which is simple and very similar to jQuery. You can use this to manipulate html string from php

Nauphal
  • 6,194
  • 4
  • 27
  • 43
0

you can try this jquery

$("img").each(function(){
           var htm = $(this).html(); 
           htm = '<div class="miricle_image">'+htm+' div</div>';
           $(this).html(htm);  
    });

DEMO

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
0

try to test this code

<?php

$text = "<img src='abc.jpg'><img src='abc.jpg'><img src='abc.jpg'>";

$text = preg_replace("/<img([^>]*)>/i", '<div class="miricle_image"><img\\1></div>', $text);

echo $text;

?>

and demo here https://eval.in/178421

0

Try using PHP HTML parser like: https://github.com/paquettg/php-html-parser and try replacing img node with wrapped one.

smoq
  • 130
  • 2
  • 6