Home > other >  Match everything until first slash, get numbers between 1º slash and dash
Match everything until first slash, get numbers between 1º slash and dash

Time:11-04

I need to get only the numbers from a URL between the first slash of a URL and the immediate dash after those numbers.

In other words

If this is my URL: http://galleries.video.com/39061-all_other-text, I need a regex to get only the numbers 39061

CodePudding user response:

Use preg_match to extract only numbers

<?php 
$string = 'http://galleries.video.com/39061-all_other-text';
preg_match_all('!\d !', $string, $matches);
print_r($matches); 

//Array ( [0] => Array ( [0] => 39061 ) )
?> 

You can explode $string to get only last part of URL

  • Related