0

I'm using CKEDITOR for saving html content. After saving the html to the database, my image source isn't correct for display. For example I save the following html code to database:

HELLO WORLD<br \/>
\n\n<hr \/>\n
<img alt=\"\" src=\"http:\/\/localhost\/MM\/uploads\/1370293869_toranj.jpg\"
 style=\"height:244px; width:201px\" \/><br \/>\n

The src must be like below:

src=\"http://localhost/MM/uploads/1370293869_toranj.jpg\"

I want to remove \ from src starting from http to end of src url.

UPDATE:

after any try with str_replace and preg_replace i can use this below code for remove \ from src and replace / width \ for create correct image src to displage

echo str_replace('\\','/', preg_replace('/\/+/i', '', $html));
  • Before pressing the `submit` button, can you review your question ? There are so much typos which is not really "cool". – HamZa Jun 04 '13 at 08:25
  • sending data and save to database can successfull insert. i want to replace after fetch from database –  Jun 04 '13 at 08:30
  • 3
    You should not be saving escaped sequences in your database in the first place. – Jon Jun 04 '13 at 08:39

1 Answers1

1

Why not using str_replace() combined with preg_match()?

preg_match( '/src="([^"]*)"/i', $imagetag, $array ) ;
str_replace('\\"','"', $array[1] ) ; //not tested

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) http://php.net/manual/en/function.str-replace.php

I don't know actually why and how you store the escaped sequences, but maybe this is also a helpful function for you: http://www.php.net/manual/en/function.htmlspecialchars-decode.php

Maybe interesting too: CKEditor is escaping html elements

Community
  • 1
  • 1
Karl Adler
  • 15,780
  • 10
  • 70
  • 88
  • because i cant use preg_replace and str_replace for src –  Jun 04 '13 at 08:40
  • @mahdipishguy that doesn't make sense at all. You want to remove something, you don't want to match. – HamZa Jun 04 '13 at 08:45
  • @abimelex, my correct result is: `echo str_replace('\\','/', preg_replace('/\/+/i', '', $html));` –  Jun 04 '13 at 09:41
  • 1
    super. Just for interest, did you also tried htmlspecialchars-decode() or to setup the editor like mentioned here? http://stackoverflow.com/questions/9507250/ckeditor-how-to-i-convert-all-html-entities/9509108#9509108 – Karl Adler Jun 04 '13 at 09:56