Hi know there's already a lot of question about this so far. But I've tried a lot of them and can't get it quite where I need it.
I need a regex that will extract a youtube url from a string that contains an iframe.
Sample text:
<p>
</p><p>Garbage text</p><p><iframe width="560" height="315" src="//www.youtube.com/embed/PZlJFGgFTfA" frameborder="0" allowfullscreen=""></iframe></p>
Here's the regex I come up with :
(\bhttps?:)?\/\/[^,\s()<>]+(?:\([\w\d]+\)|(?:[^,[:punct:]\s]|\/))
I'm using it on a function and it returned an empty array. Do someone have an idea what's wrong with my function ?
function extractEmbedYT($str) {
preg_match('/(\bhttps?:)?\/\/[^,\s()<>]+(?:\([\w\d]+\)|(?:[^,[:punct:]\s]|\/))/', $str, $matches, PREG_OFFSET_CAPTURE, 0);
return $matches;
}
EDIT 1 : Changed capture group in my regex so it don't capture last the last char
EDIT 2 : Added some PHP Code to put in context, since it's working in Regex101 but not on my script.