0

is there a way to check is URL sent in request complete. For example, I want to store in my database only links which are complete (with protocol) like:

https://google.com

and if a user is sending a link without this protocol https:// I want to merge it automatically and if it is there I don't want to add it one more time. For example, if the user is really sending in request full link and code add it automatically one more time it would be like https://https://google.com

Currently, I have this part where I add protocol but I don't have the part where I am checking is the protocol there.

    ...
    protected function prepareForValidation()
    {
        if(!empty($this->site_facebook))
        {
            $this->merge(['site_facebook' => 'https://' . $this->site_facebook]);
        }
    ...
Sead Silajdzic
  • 298
  • 1
  • 4
  • 21

1 Answers1

0

you can parse the url and can add create new url like this

$url = "google.com";

$url = parse_url($url);

$finalUrl = ($url['scheme'] ?? 'http') . "://" . (isset($url['host']) ? $url['host'] : $url['path']);

dd($finalUrl);

ref link https://www.php.net/manual/en/function.parse-url.php

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33