0

I have a problem with integrations in the php code in javascript within single quotes

I need to put the variable $video that will get the id of a YT video, instead of the id of the video **VARIABLEVIDEOHER**E, but I tried all ways to put single and double quotes did not work to integrate more, someone could give me a light

<?php $video = 'BYN-DEM7Mzw';
$desc = '<script type="text/javascript">
function youtubeFeedCallback( data ){
document.writeln( data.entry[ "media$group" ][ "media$description" ].$t.replace( /\n/g, "<br/>" ) + "<br/>" ); }
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/VARIAVELVIDEOAQUI?v=2&amp;alt=json-in-script&amp;callback=youtubeFeedCallback"></script>';
echo $desc; ?>
Fluffeh
  • 33,228
  • 16
  • 67
  • 80

2 Answers2

1

Use json_encode() to turn the string into a JavaScript literal, and then add it to the rest of the string.

var foo = "bar" + <?php echo json_encode('quux'); ?>;
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Is this what you mean?

<?php $video = 'BYN-DEM7Mzw';
$desc = '<script type="text/javascript">
function youtubeFeedCallback( data ){
document.writeln( data.entry[ "media$group" ][ "media$description" ].$t.replace( /\n/g, "<br/>" ) + "<br/>" ); }
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/'.$video.'?v=2&amp;alt=json-in-script&amp;callback=youtubeFeedCallback"></script>';
echo $desc; ?>

It seems you are just trying to insert the variable in a string, not really a javascript problem. As a side note, copying code from the internet without actually understanding what it does is genuinely bad, and generally leads to unmaintainable spaghetti code that breaks easily, but that's a different issue.

Mahn
  • 16,261
  • 16
  • 62
  • 78
  • just that you need, worked perfect I ask forgiveness because I really do not understand programming. – Maikel Richard Jul 19 '12 at 01:16
  • Well then don't program :) If you want to mess around with PHP you will have to learn how to program, PHP is a programming language and saying I don't understand programming is no excuse to produce code that breaks. If you wanted to drive a car, normally you would learn how to drive car first before driving, right? otherwise you would crash the car. – Mahn Jul 19 '12 at 01:22
  • so they can understand my question, I need to get the description of a video from youtube through the id of the video and save this description in a custom field with the same formatting youtube – Maikel Richard Jul 20 '12 at 14:57