5
<?php echo $videoFirstArray['fileToUpload']; ?>

This field contains the ifrmae,something like this

<iframe width="560" height="315" src="https://www.youtube.com/embed/ISMzgunUono" frameborder="0" allowfullscreen></iframe>

From this i want this "ISMzgunUono".I've searched the forum and follwed the link but didn't find the any way to keep my 'fileToUpload' field inside the pregmatch. Some of the links that i follwed: Get YouTube video id from embed iframe code and this link gives an error how to get Video id from iframe of youtube in php or cakephp Expecting replies soon:

Community
  • 1
  • 1
andy
  • 97
  • 1
  • 9
  • I want the video id from the database field fileToUpload.Php will be better i think,but if possible kindly send the code in JS also for my better understaing of JS – andy May 30 '16 at 09:41
  • Try this http://stackoverflow.com/questions/24378286/how-to-get-the-id-from-iframe – Ravi Kumar May 30 '16 at 09:44
  • Why didn't this work? https://regex101.com/r/vY6eV7/15 – Peon May 30 '16 at 09:50

3 Answers3

4

Strictly speaking, in PHP, you should be able to run:

preg_match('/youtube.com\/embed\/([A-Za-z0-9-]+)/', $videoFirstArray['fileToUpload#'], $matches);
if($matches[1]) {
    echo $matches[1];
}

And it should echo ISMzgunUono. This is untested, though! Let me know if it works.

Forest
  • 938
  • 1
  • 11
  • 30
  • 1
    Solved,Thnx for all the help,u all kept me moving in the right direction.The second link from ravi solved the problem – andy May 30 '16 at 10:13
  • 1
    Thnx to all,you all saved me today,got good comments from my manager.Cheers to all. – andy May 30 '16 at 10:22
3

With javascript and regular expressions you can do like this:

var url = document.getElementById('myIframe').src,
    regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/,
    videoId = url.match(regExp);

if (videoId && videoId[1].length === 11) {
    console.log(videoId[1]);
}
<iframe id="myIframe" width="560" height="315" src="https://www.youtube.com/embed/ISMzgunUono" frameborder="0" allowfullscreen></iframe>
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

You can try this:

<?php

$string = $videoFirstArray['fileToUpload'];
$pattern = '!youtube.com/embed/([^"]+)!i';
preg_match($pattern, $string, $match);
print_r($match);

?>

The expected output should be something like this :

Array
(
    [0] => youtube.com/embed/ISMzgunUono
    [1] => ISMzgunUono
)

Btw this was taken by how to get Video id from iframe of youtube in php or cakephp

Community
  • 1
  • 1
bearnik
  • 1
  • 1