user1130861
user1130861

Reputation: 45

Get part of URL with regular expression

Can anyone tell me why this is not working? I just want to get test.jpg from the url

$html = 'this/large/test.jpg)';
$str = $html;
preg_match('/\/large\/(\d+)\)/',$str, $matches);
echo $matches[1];

Upvotes: 0

Views: 359

Answers (2)

Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47913

\d matches only digits (see here). Use (.+) instead.

Upvotes: 1

mathematical.coffee
mathematical.coffee

Reputation: 56915

Because \d matches decimals (ie [0-9]) and test.jpg is not.

Try preg_match('/\/large\/(.+)\)/',$str,$matches);.

Upvotes: 2

Related Questions