elaineee elaineee - 2 months ago 12
PHP Question

PHP custom regex in url

I am new in Stackoverflow and I have a simple question: I need to create a regex to match a custom patterns in string. I want to create a regex to detect this url:

/post-title-92581_1.html

where $match[0] = 92581 and $match[1] = 1


I created this regex
((\-[0-9]+)_([1-2]{1}).html)
works fine,but this returns:

$match[0] // -92581
$match[1] // 1


if i change string to
/post-title-925-81_1.html
i get

$match[0] // -81
$match[1] // 1


I only want to get the FIRST MATCH
(-925)
without "
-
".

Answer

Put the - outside of the brackets...

-([0-9]+)_([1-2]{1}).html

Everything inside the brackets gets returned as a match!