Are you looking for this function
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?
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⏎
Please or to participate in this conversation.