bpy
bpy

Reputation: 1180

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

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

Upvotes: 0

Views: 37

Answers (1)

Flak
Flak

Reputation: 2670

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

Upvotes: 2

Related Questions