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.

4
  • 1
    href="http://'.$_POST['domain'].'". Simples! Commented Aug 1, 2012 at 16:23
  • Have you tried ...href="http://' . $_POST['domain'] . '...? Commented Aug 1, 2012 at 16:23
  • possible duplicate of Add http:// prefix to URL when missing Commented Aug 1, 2012 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. Commented Aug 1, 2012 at 16:28

3 Answers 3

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>';
Sign up to request clarification or add additional context in comments.

Comments

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>';

1 Comment

Thanks I thought I had tried that but I must have done a typo first time. thanks.
0
href = "http://'.$_POST['domain'].'";

1 Comment

Thanks I thought I had tried that but I must have done a typo first time. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.