PDA

View Full Version : Youtube RegExp Compatible with preg_match_all


GHRake
05-18-2016, 10:49 PM
Hello people of vbulletin, I've decided it's time to learn some php and write a vbulletin plugin.

So far so good:
https://images-1.discordapp.net/eyJ1cmwiOiJodHRwOi8vaS5pbWd1ci5jb20vckN0cWN6ay5wbm cifQ.bNvhAZoiLigHDhljt4R9i81gcAI.png

But I'm having trouble with getting a rexexp to work with preg_match_all()
I'm getting the dreaded
"PHP Warning: preg_match_all(): Delimiter must not be alphanumeric or backslash"

When I use:

if (stripos($string, 'youtu') !== false)
{
$regex = 'https?:\/\/((.+\.youtube\.com\/watch\?v=)|(youtu\.be\/))([a-zA-Z0-9-_]+)';
preg_match_all($regex, $string, $matches);
if (!empty($matches))
{
$og_array['og:video'] = $matches[2][0];
}
}


Any words of wisdom? TIA

cellarius
05-19-2016, 04:35 AM
Just as the error states: You don't have any valid delimiters for your expression in $regex. Try:

$regex = "/https?:\/\/((.+\.youtube\.com\/watch\?v=)|(youtu\.be\/))([a-zA-Z0-9-_]+)/";

Paul M
05-19-2016, 07:52 AM
As well as /, vbulletin uses # as the delimiter as well.

GHRake
05-19-2016, 03:21 PM
Ah yes, thank you gentleman for your assistance