Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

How to parse url the best way

Hey!

I have a websites table with the following structure (showing only the necessary for my question columns):

web_seo_url, web_link

where web_seo_url is actually the domain name of the website (example: domain.com) and the web_link is the actual URL address of the website.

I have a search form that is looking through these websites and showing results.

Here is my search action

        if(filter_var($request->domain, FILTER_VALIDATE_URL)) {
            $freedomain = parse_url($request->domain);
        }

        if(filter_var($request->domain, FILTER_VALIDATE_DOMAIN)) {
            $freedomain['host'] = $request->domain;
        }

        if($freedomain !== true) {
            $freedomain['host'] = $request->domain;
        }

        $freedomain['host'] = str_replace('www.', '', $freedomain['host']);

        dd($freedomain['host']);

        if(Websites::where("web_seo_url", $freedomain['host'])->exists()) {

            return Redirect()->to('/reviews/' . $freedomain['host']);

        } elseif(Websites::where("web_link", $request->domain)->exists()) {

            return Redirect()->to('/reviews/' . $request->domain);

        } else {
                
                     // ... SOME OTHER CODE

        }

but that code is not what I need. Here's what is needed to happen

If there is a provided URL (no matter what URI it has, it might be https://domain.com/index.php?page=3) that exists in the web_link (as a website address https://domain.com) or in web_seo_url (as a domain name domain.com) - redirect to /reviews/domain.com

I have no idea how to parse the input to get the domain name correctly without any bugs. Can you guys help me?

0 likes
20 replies
lat4732's avatar
Level 12

@sr57 The general problem is that I can't catch its errors because when providing just a word like test it will result in cannot parse url: test for example. If there is a way to catch its errors will be pretty good.

sr57's avatar

@Laralex

don't understand

$url = 'test';var_dump(parse_url($url));
array(1) {
  ["path"]=>
  string(4) "test"
}
lat4732's avatar
Level 12

@sr57 My bad. There was some misunderstanding. Nevermind. So look at this:

$freedomain = parse_url($request->domain);

if(!array_key_exists("host", $freedomain)) {
     return Redirect()->to('/companies?searchWord=' . $request->domain);
}

// ....

right now when I pass https://test.com I'm being redirected to /companies?searchWord=https://test.com and that's not what I want. Why does array_key_exists() not working as expected? when I dd() - https://test.com there is a host key.

sr57's avatar

Why does array_key_exists() not working as expected?

Should do

dd($freedomain) ?

lat4732's avatar
Level 12

array_key_exists('host', $freedomain) plays the role of distinguishing whether URL or PLAIN TEXT is entered. If the array don't have host key its obviously a plain text -> which means redirect to /companies?searchWord={PLAIN_TEXT}.

sr57's avatar
sr57
Best Answer
Level 39

@Laralex

don't undertand, with your code $freedomain is an array or False

https://www.php.net/manual/fr/function.parse-url.php

never a plain text ...

and so array_key_exists works as expected

$url = 'https://test.com';$freedomain=parse_url($url);var_dump($freedomain);if ( !array_key_exists("host",$freedomain) ) echo "Out"; else echo "In";
array(2) {
  ["scheme"]=>
  string(5) "https"
  ["host"]=>
  string(8) "test.com"
}
In⏎
lat4732's avatar
Level 12

@sr57 Ok, let me provide you a list of what I need to happen with the text that the user entered.

  1. If it is a link - check if there is such a web_link in DB and if yes - redirect to url('/reviews')
  2. If it is a domain name (NOT A LINK) - check if there is such web_seo_url in DB and if yes - redirect to url('/reviews')
  3. If not a link or domain name (JUST A PLAIN TEXT OR NOT EXISTING URL/DOMAIN NAME IN DB) - redirect to redirect to url('/companies?searchWord=' . $request->domain)
sr57's avatar

You miss some cases ... for instance it's a link but not DB / web_link?

You should probably simplify your logic :

in DB / web_link ? yes ....

No : to be continue

lat4732's avatar
Level 12

@sr57 If it's a link but not in DB then I add it in DB. It's really hard man.. I need to check for everything but I have no idea how - if it's a link / if it's a domain name / if its a plain text...

sr57's avatar

@Laralex

Reread my 1rt answer, with parse_url you can test all the cases

you have to test the keys : host, path, ... and not only host

Test the result of parse_url of this function on your different cases

lat4732's avatar
Level 12

@sr57 That's what I've built so far

if(filter_var($request->domain, FILTER_VALIDATE_URL) || filter_var($request->domain, FILTER_VALIDATE_DOMAIN)) {

    $domain = parse_url($request->domain);

    if(!array_key_exists('host', $domain)) {
        $domain['path'] = str_replace('www.', '', $domain['path']);
        if(Websites::where('web_seo_url', $domain['path'])->exists()) {
            return Redirect()->to('/reviews/' . $domain['path']);
        }
        if(filter_var($domain['path'], FILTER_VALIDATE_DOMAIN)) {
            return Websites::createWebsite($domain['path']);
        } else {
            return Redirect()->to('/companies?searchWord=' . $request->domain);
        }
    } else {
        $domain['host'] = str_replace('www.', '', $domain['host']);
        $freeURL_original = $domain['scheme'] . "://" . $domain['host'];
        $freeURL_http = str_replace("https", "http", $freeURL_original);

        if(Websites::where('web_link', $freeURL_original)->exists() || Websites::where('web_link', $freeURL_http)->exists()) {
            return Redirect()->to('/reviews/' . $domain['host']);
        } else {
            return Websites::createWebsite($domain['host']);
        }

    }

    
} else {
    return Redirect()->to('/companies?searchWord=' . $request->domain);
}
sr57's avatar

That's what I've built so far

So that's should work ...

If not it's a question a logic, nothing to do with your initial question, close this one and open an onother one describing your problem

1 like
lat4732's avatar
Level 12

Ok, I have another question. I'll take the current topic URL without the protocol and www.

laracasts.com/discuss/channels/laravel/how-to-parse-url-the-best-way

Which php function can determine if this is a URL or not? And if so, how to parse it?

parse_url() returns

array:1 [▼
  "path" => "laracasts.com/discuss/channels/laravel/how-to-parse-url-the-best-way"
]
lat4732's avatar
Level 12

@sr57 So you mean I shouldn't care for a string that doesn't begin with http or https? I should treat it as a plain text?

sr57's avatar

@Laralex

it's not an url, no scheme key in the result of parse_url

lat4732's avatar
Level 12

@sr57 Ok, so I'll just treat it as a plain text and nothing to do with URL. Thanks!

sr57's avatar

@Laralex

Good :-) So it was not a question of logic, but of definition.

1 like

Please or to participate in this conversation.