Jump to content

How Would I


walapu

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?

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...