Regex to filter int from url

I am trying to filter the url value.

The URL looks like this:

Http and colon // userimages -akm.imvu.com/catalog/includes/modules/phpbb2/images/avatars/ 145870556 _47076915459092eafd7b69.jpg

Now I am trying to get just the following part from the url: 145870556

I was thinking about using regex. But I won't get a valid regex next to this:

^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$

      

Is there a better regex to use?

+3


source to share


1 answer


If the file name of the image always follows the same format <timestamp>_<hex-value>.<extension>

, then you don't need to match the entire URL.

$url = 'http://userimages-akm.imvu.com/catalog/includes/modules/phpbb2/images/avatars/145870556_47076915459092eafd7b69.jpg';
preg_match_all('~\/(\d+)_.*$~', $url, $matches);

// $matches[1] = '145870556';

      



https://regex101.com/r/vsDnoj/2

+1


source







All Articles