Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
i'am trying to embed youtube link using php but its not working help me here

Code: Select all
<cms:editable name="video1" type="textarea" no_xss_check="1" />
                       
                                
<cms:php>
   echo ($url = '<cms:show video1 />');
   
     preg_match('/[\\?\\&]v=([^\\?\\&]+)/', $url, $matches);
     $id = $matches[1];
   
</cms:php>
<iframe id="ytplayer" type="text/html"
    src="https://www.youtube.com/embed/<cms:php> echo ($id);</cms:php>"  frameborder="0" allowfullscreen></iframe>

I suspect that your $id gets discarded after the php block finishes. You need to wrap that PHP block with a <cms:set.../> or <cms:capture /> statement to save the variable into CMS's scope, try something like this:

Code: Select all
<cms:set id="
<cms:php>
    preg_match('/[\\?\\&]v=([^\\?\\&]+)/', $url, $matches);
    echo $matches[1];
</cms:php>
"/>


and then to show it:

Code: Select all
<iframe id="ytplayer" type="text/html" src="https://www.youtube.com/embed/<cms:show id />"  frameborder="0" allowfullscreen></iframe>
not working
@gopi,

Go step-by-step -
1. Make sure your PHP code is working as you expect it to work. Just use the following on the page and see if the video output is properly extracted -
Code: Select all
<cms:php>
    $url = '<cms:show video1 />';
    preg_match('/[\\?\\&]v=([^\\?\\&]+)/', $url, $matches);
    echo $matches[1];
</cms:php>

2. Once that is ascertained, instead of outputting the ID, save it into a global Couch variable named 'my_video_id' as follows -
Code: Select all
<cms:php>
    global $CTX;
   
    $url = '<cms:show video1 />';
    preg_match('/[\\?\\&]v=([^\\?\\&]+)/', $url, $matches);
    $CTX->set( 'my_video_id', $matches[1], 'global' );
</cms:php>

<cms:show my_video_id />

Again make sure you can see the extracted ID being outputted.

3. If the step above works, you can go ahead use the 'my_video_id' variable e.g. -
Code: Select all
<iframe id="ytplayer" type="text/html" src="https://www.youtube.com/embed/<cms:show my_video_id />" ...

Hope this helps.
4 posts Page 1 of 1