Argh-- regular expressions make me crazy, I've just spent 20 minutes trying to get this to fly and I'm having no luck. And I know someone here will be able to pop this out in like 2 seconds! :-)
Here's a sample source URL: https://rumble.com/v30sqt-oreo-ice-cream-cake.html
I want to extract the "v30sqt" characters. Actually, I want to extract any characters after "rumble.com/" and before the first dash. It might be alphanumeric, it might be all letters, it might be longer than 6 characters, etc. That's the video ID.
This is for php preg_match
.
You can simply use parse_url
instead of using regex
along with explode
and current
function like as
$url = "https://rumble.com/v30sqt-oreo-ice-cream-cake.html";
$parsed_arr = explode("-",ltrim(parse_url($url, PHP_URL_PATH),"/"));
echo current($parsed_arr);
or
echo $parsed_arr[0];
Go for:
<?php
$url = "https://rumble.com/v30sqt-oreo-ice-cream-cake.html";
$regex = '~rumble\.com/(?P<video>[^-]+)~';
if (preg_match($regex, $url, $match)) {
echo $match['video'];
# v30sqt
}
?>
With a demo on ideone.com.