Jump to content

How Would I


walapu
 Share

Recommended Posts

Guest Damyan Petkov
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

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?

Edited by walapu
Link to comment
Share on other sites

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.

Edited by Reiyan
Link to comment
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...