I'm trying to match a url that can be inputted a few ways.
The url is an iTunes link, and I'm trying to source the id
out of the url.
I want to know is it possible to have an expression strip everything but the id#########
and then use str_replace
to remove the id.
I have started here: http://www.phpliveregex.com/p/dWp but am no expert in expressions.
The main point to the above url is that if it is already stripped from the iTunes url and the id, is there a way to store it in the array?
Or is there a way to say:
if( THE STRING IS NOT EMPTY ) {
if( THERE ARE MATCHES IN THE PREG_MATCH ) { $VAR = $MATCHES; }
if( THERE ARE NO MATCHES IN THE PREG_MATCH ) { $VAR = $ORIGINAL_STRING; }
}
You don't need to use a regex since there a PHP function to parse URLs (parse_url
) and since you already know that there's an id in the last directory of the url (in other words, you don't need to check the format):
$path = parse_url($url, PHP_URL_PATH);
$lastdir = array_pop(explode('/', $path));
$result = ltrim($lastdir, 'id');