Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add http:// prefix to URL when missing

Hello I have a very simple code

<a href="'.$aProfileInfo['Website'].'" target="_self">     <div class="callButton">Website</div> </a> 

The problem is that if the user does not enter http:// the link will then point to my website and not to the external website as it should.

How do I check in PHP if the user has not entered http:// and automatically add it when it is not there?

like image 480
DiegoP. Avatar asked Jun 05 '11 01:06

DiegoP.


People also ask

How do I add an HTTP to a URL?

Method 2: This method use parse_url() function to add http:// if it does not exist in the url. echo $url ; ?> Method 3: This method use strpos() function to add the http:// if it does not exist in the url.

What is the http prefix called?

As we stated above, WWW is a prefix used to indicate that a website is using HTTP to communicate.


1 Answers

I think you'd better use the built in function parse_url() which returns an associative array with its components

something like this will work for you:

 if  ( $ret = parse_url($url) ) {        if ( !isset($ret["scheme"]) )        {        $url = "http://{$url}";        } } 
like image 72
David Avatar answered Oct 07 '22 02:10

David