PHP valid url, including subdomains and also subpath

I need to check if a given string is valid. For me, the following and any other similar combinations are all valid URLs

'https://example.com/api/',
'https://www.example.com/test-subpath',
'https://www.example.com',
'example.com/test/page',
'www.example.com',
'www.subdomain.example.com',
'https://www.subdomain.example.com',
'subdomain.example.com',
'http://subdomain.example.com',
'https://subdomain.example.com'

while

'user-service/api/'

is invalid. I tried parse_url() and filter_var($url, FILTER_VALIDATE_URL) methods but non worked.

Thanks in advance.