3

I'm trying to filter out a value of a url.

The url looks like the following:

http://userimages-akm.imvu.com/catalog/includes/modules/phpbb2/images/avatars/145870556_47076915459092eafd7b69.jpg

Now i'm trying to only receive the following part from the url: 145870556

I thought about using a regex. But i won't get a working regex beside this one:

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

Is there a better regex to use?

2

1 Answer 1

1

If the image filename 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

Sign up to request clarification or add additional context in comments.

3 Comments

.*$ is redundant there.
For this example, perhaps. But I prefer to keep the regex as tight as possible to prevent possible mis-matches. Each to their own.
.*$ does not make it any tight though. Just bloated (and slower) :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.