views:

58

answers:

2
+1  Q: 

Form Fix in php

I have a website form that collects url of users to store in a database. They should not enter the http:// with their URL however many and the result is that when their url is displayed it looks like this

http;//http://www.foo.com I need the form to strip it or ignore it or what ever you think is the best way to handle it.

thanks

+3  A: 

Use this on the url given by the user:

$url=str_replace("http://","",$_POST['url']);
//Where $_POST['url'] is the users input

This function takes an argument and replaces all occurrences of that argument within a string. More on this function here.

Sam152
+1  A: 

You should do two things!

1 - Clean up your database and replace all http://http//example.org entries so that your database is fine with your convention (http://example.org, protocol is included in URL).

// Something like this ...
UPDATE table SET field = REPLACE(field, 'HTTP://HTTP://', 'HTTP://');

2 - After a user submitted his URL, you should check for the string "http://".

$url = trim('http://example.org');
if (0 !== strpos($url, 'http://')) {
    $url .= 'http://' . $url;
}
Philippe Gerber