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; ?>

2 Answers 2

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'); ?>;
Sign up to request clarification or add additional context in comments.

4 Comments

I'm new to php and javascript, if it is not asking too much could put together exactly how the code would get your answer, thank you
Can you not adapt the example given in the answer?
json code found on the web, do not quite understand, I just want to adapt it receives the id of the video of a variable and do not know how to do
Clever answer though. OP clearly didn't have a javascript problem but it's interesting that you can json_encode values directly.
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.

3 Comments

just that you need, worked perfect I ask forgiveness because I really do not understand programming.
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.