I want to capture the first slug in the URL only if the URL has one slug, and ignore if it has more than one slug or section in the URL. For example:
https://example.com/path/some-slug-with-numbers-int ✅
example.com/path/some-slug-with-numbers-int/ ✅
example.com/path/some-slug-with-numbers-int/external/slug ❌ // ignore and don't capture
Trailing slash and no HTTP protocol is allowed.
My regex: https://regex101.com/r/0JYHMM/1/
preg_match('/example\.com\/path\/(.*?)(\/|$)(?!\w)/', $input, $match);
if (!empty($match)) {
$slug = $match[1];
// $slug == 'some-slug-with-numbers-int'
}
It should capture the first and second URLs I posted, but my regex captures all of them.