NamelessNemo
NamelessNemo

Reputation: 13

getting rid of Http on a <a> tag

I have code that puts a web address into a variable to be used in a <a> tag. The issue is when people input http://ect with their address then the <a> tag adds another http:// on to it, then the link wont work. Is there a way to remove http:// from the <a> tag.

example

<a href="http://<?php echo ($data->website); ?>" target="_blank">Link</a> 

If $data = "www.example.com" it'll work.
But $data = "http://www.example.com" it wont.

Thank you in advance. I know I'm a novice.

Upvotes: 1

Views: 54

Answers (4)

Labbed
Labbed

Reputation: 31

You shouldn't remove it, since a lot of URL:s begin with https. Instead, you should only add it if it's missing. Check if the user's input contains "://", and if it doesn't, add "http://" in the beginning of the URL.

Upvotes: 1

Sam
Sam

Reputation: 1559

Since restricting users is not a good idea, By a very simple code you can accept both http:// included and not included.

$newaddress = str_ireplace("http://","", $address);

this code will remove http:// from the address if it has.

Upvotes: 0

Try Following,

$url = remove_http($data->website);

Add following Funciton in your file.

function remove_http($url) {
   $disallowed = array('http://', 'https://');
   foreach($disallowed as $d) {
      if(strpos($url, $d) === 0) {
         return str_replace($d, '', $url);
      }
   }
   return $url;
}

Upvotes: 0

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

Replace your http:// with emptyString

Try like this

$data->website=str_replace("http://","",$data->website);

Upvotes: 2

Related Questions