3

When I save a youtube embed iframe into my database it adds a bunch of slashes when i recall it. Like so below.

<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/ycHXRWRKrdA?rel=0\" frameborder=\"0\" allowfullscreen></iframe>

its is being stored in a longtext type. what would remove the slashes when I recall it from the database or prevent slashes being stored in the first place

Mike
  • 37
  • 1
  • 6
  • What technology do you use? You must have a security option enabled that automatically escapes the strings before persisting them into the db (which is not bad actually). One option is to remove those backslashes using regex (e.g. `.replaceAll("\\\\", "")`). – sp00m May 23 '12 at 23:20
  • 2
    Nothing to do with the question, but why would you store that in such a nasty way? Why don't you store the video's ID only?! – Samy Dindane May 23 '12 at 23:26
  • I assume that this is happening because you use an older version of php than 5.3 and in there the config flag [`magic_quotes_gpc`](http://www.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc) is set to `true`, which makes php do automatic quote escaping on all get, post and cookie values. – s1lence May 23 '12 at 23:27
  • it just seemed logical thing to do. I'm using it to store posts I make with a CMS. what would be a better way? – Mike May 23 '12 at 23:33

3 Answers3

6

Don't save the HTML like that - just save the youtube unique ID and then spit out the HTML and plug in the ID from the database.

PHP Regex to get youtube video ID?

Community
  • 1
  • 1
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
1

if you use PHP, you can use stripslashes().

Alternatively you can base64 encode / decode the code.

Pethical
  • 1,472
  • 11
  • 18
1

once and for all add this in your .htaccess:

# disable the automatic-quote-removing from requests
php_value magic_quotes_gpc off

you didnt say if you save the youtube url in a JSON in database. if yes, replace possible quotes via:

str_replace('\"','\\"',$url);

also, of course its better idea to save just the youtube code as:

$eurl = explode("/",$url);
$eurl[sizeof($eurl)-1];     // <-- to database