How can I check the start of a string in php?

I need something like this

  • user enters a website link

  • I need to check the link if the link does not start with 'http: //' I want to add the link "http: //" to the link.

how can i do this in php?

+3
source share
4 answers
if (stripos($url, 'http://') !== 0) {
   $url = 'http://' . $url;
}
+11
source

I recommend a slight improvement over tom

if (0 !== stripos($url, 'http://') && 0 !== stripos($url, 'https://')) {
   $url = 'http://' . $url;
}

However, this will ruin the links that use other protocols (ftp: // svn: // gopher: // etc.)

+7
source
if (!preg_match("/^http:\/{2}/",$url)){
    $url = 'http://' . $url;
}
+3

, . URI "" (http, ftp ..) " ". , - () mailto , .

+2

Source: https://habr.com/ru/post/1699281/


All Articles