walapu Posted July 17, 2009 Share Posted July 17, 2009 I am looking for how to add http://www. to a url on submit if it doesnt have it yet in the form. Link to comment Share on other sites More sharing options...
NurAzije Posted July 17, 2009 Share Posted July 17, 2009 You can do that with JavaScript. Use the match() method to check the URL. Link to comment Share on other sites More sharing options...
walapu Posted July 17, 2009 Author Share Posted July 17, 2009 I have no experience at all in a javascript. I just know the bare minimum. Could you give me an example? Link to comment Share on other sites More sharing options...
Guest Damyan Petkov Posted July 17, 2009 Share Posted July 17, 2009 I have no experience at all in a javascript. I just know the bare minimum. Could you give me an example?Hello walapu,you may use PHP function to check:string substr ( string $string , int $start [, int $length ] )Description:Returns the portion of string specified by the start and length parameters. <?php$rest = substr("abcdef", 0, -1); // returns "abcde"$rest = substr("abcdef", 2, -1); // returns "cde"$rest = substr("abcdef", 4, -4); // returns ""$rest = substr("abcdef", -3, -1); // returns "de"?> Simple code to your case: <?php$url_to_check = "http://w3schools.invisionzone.com/";If (substr($url_to_check, 0, 11) == "http://www.") { echo "true";} else { echo "false";}?> Link to comment Share on other sites More sharing options...
walapu Posted July 17, 2009 Author Share Posted July 17, 2009 How about adding it if it isnt there? Maybe like this? $httpwww_check = httpwww_check($_POST['url']);function httpwww_check($field) { if(substr($field, 0, 11) == "http://www.") { return TRUE; } else { return FALSE; } } Then were I want to add it $url = (here)$_POST['url'];but how? Link to comment Share on other sites More sharing options...
danm68 Posted July 17, 2009 Share Posted July 17, 2009 going off the example you posted.. if(!$httpwww_check) //if $httpwww_check is equal to false, meaning there is no 'http://www.'{ $url = "http://www.{$_POST['url']}"; //put it all together in a string. //if that doesn't work, store $_POST['url'] in a variable, then just add in variable. $postUrl = $_POST['url']; $url = "http://www.$postUrl";} Either of those should work and hope that was what you were after. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 17, 2009 Share Posted July 17, 2009 You would normally use strpos to check if a substring exists in a string. If strpos returns 0, that means the string begins with the substring. if (strpos($_POST['url'], 'http://www') !== 0) $_POST['url'] = 'http://www.' . $_POST['url']; You might also find this useful:http://www.php.net/manual/en/function.parse-url.php Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.