Search code examples
phpjavascriptquotesdouble-quotesquote

Integration PHP + Javascript


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

Solution

  • 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.