0

How can I add a 'http://' component to a post variable so that it automatically adds that part so if someone submits: 'www.google.com' is will register as http://www.google.com. Heres the code I have so far:

$domain = '<a target="_blank" href="' .$_POST['domain'] . '">' . $_POST['domain'] .'</a>';

so where do I insert the 'http://'? I have tried a few variations with no success perhaps someone can enlighten me. Thank you.

user1559811
  • 437
  • 3
  • 10
  • 26
  • 1
    `href="http://'.$_POST['domain'].'"`. Simples! – Joe Green Aug 01 '12 at 16:23
  • Have you tried `...href="http://' . $_POST['domain'] . '...`? – Palladium Aug 01 '12 at 16:23
  • possible duplicate of [Add http:// prefix to URL when missing](http://stackoverflow.com/questions/6240414/add-http-prefix-to-url-when-missing) – Manse Aug 01 '12 at 16:24
  • 2
    There are a couple of correct answers up now, but note that this has nothing to do with it being a POST variable. You an assign the POST variable to a regular variable and manipulate it in any way you want. – octern Aug 01 '12 at 16:28

3 Answers3

1
function startsWith($haystack, $needle) {
  $length = strlen($needle);
  return (substr($haystack, 0, $length) === $needle);
}

$address = startsWith($_POST['domain'], 'http://') ? $_POST['domain'] : 'http://' . $_POST['domain'];

$domain = '<a target="_blank" href="' . $address . '">' . $_POST['domain'] .'</a>';
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
0

Maybe I don't understand your question, but is this what you want to do?

$domain = '<a target="_blank" href="http://' .$_POST['domain'] . '">' . $_POST['domain'] .'</a>';
Gordon Bailey
  • 3,881
  • 20
  • 28
0
href = "http://'.$_POST['domain'].'";
Adi
  • 5,089
  • 6
  • 33
  • 47
amitchhajer
  • 12,492
  • 6
  • 40
  • 53