-5

Нi guуs. I russian php noob. I have the following code:

if($rr['site']) echo '<a target="_blank" href="http://'.$rr['site'].'" class="">'.$rr['site'].'</a>';

Need to make a valid of the user a url HTTP there or not. Otherwise it outputs - http://http://siteurl

Prompt me how to do it? pliz

Maybe, there is a solution - How do I remove http, https and slash from user input in php but i need an example with my code. I would be grateful for any help.

Community
  • 1
  • 1
  • Use the function in the example you posted. `$site = remove_http($rr['site'])` –  Jun 26 '14 at 18:01

1 Answers1

0

I would do the opposite. Instead of removing http:// or https:// and then adding it again, I would just check if the string starts with http:// or https://, and add http:// only if it doesn't.

Something like:

if($rr['site']) {
    $url = preg_match('/^https?:\/\//', $rr['site']) ? $rr['site'] : 'http://'.$rr['site'];
    echo '<a target="_blank" href="http://'.$url.'" class="">'.$url.'</a>';
}
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94