Welcome to WebmasterWorld Guest from 54.242.206.44

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

validate form help

need to check how value starts

     
7:11 am on Nov 3, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts:177
votes: 0


Hello,

I need help writing a code that will check if the first characters are http. I am accepting web addresses but don't want any values entered with the [....]

Thanks,
Matt

7:40 am on Nov 3, 2009 (gmt 0)

Full Member

5+ Year Member

joined:June 21, 2007
posts: 316
votes: 0


Hi,

There's probably a much better way using regular expressions, but they're not my strong point.But if it's just a single field that is a web address you could try:


if(trim(substr($link,0,7) == "http://")){
//not valid
}else{

}

9:50 am on Nov 3, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


@ Tommybs

There's probably a much better way using regular expressions, but they're not my strong point.

Good stay away from them unless you need to use them for some reason they're often overused (even by myself, although I keep trying to break the habit) and are processor intensive, when there's often a much more efficient way to do things...

@ weddingm

There's a few ways to do what you asked... Personally, I would probably be inclined to just str_replace("http://","",$submittedURL); them rather than making the person entering them change anything, but if you want to just check the start, you could use what Tommybs posted or if(strpos($submittedURL,"http")) { /* bad URL */ }
else { /* good URL */ }

6:23 pm on Nov 3, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts:177
votes: 0


Thanks, I like the idea of just taking it out. Will the replace code above remove both HTTP:// and http:// ?
6:27 pm on Nov 3, 2009 (gmt 0)

Full Member

5+ Year Member

joined:June 21, 2007
posts: 316
votes: 0


try using str_replace("http://","",strtolower($submittedURL)); that should be case-insensitive now.
6:31 pm on Nov 3, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 9, 2006
posts:177
votes: 0


thanks!
3:06 am on Nov 4, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


Just a 'for the tool book' note:
str_ireplace("http://","",$submittedURL);

The preceding is also case insensitive, but, personally, I would use Tommybs' example with a slight variation:

$submittedURL=strtolower($submittedURL);
str_replace("http://","",$submittedURL);

The reason for the adjustment is by changing the string to lowercase and storing it as the variable you don't ever have to worry about a cap (or all caps) in the submitted URL, whether the URL is HTTP://www.example.com or, 'Oops, I hit (or like) caps lock' WWW.EXAMPLE.COM

10:29 pm on Nov 4, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 10, 2004
posts:126
votes: 0


Re: Regular expressions

and are processor intensive

This argument does not fly with me, the performance hit is going to be negligible.


when there's often a much more efficient way to do things...

This is a much better argument. Just remember there is more than one way to skin a cat. Do what you are comfortable with, if performance becomes an issue and is traced to your regex you can easily refactor your code.

JC

3:43 am on Nov 5, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


I am accepting web addresses but don't want any values entered with the [.<...]

Do you also want to nix https? :-)

Something you probably can't do with string functions without using an "or" and two conditions (or maybe ye' can, too tired to look it up . . . )

$string = preg_replace('/https*\s*:\/\s*\//i','',$string);

The * means "zero or more" so will work for both http or https, i modifier makes it case insensitive: [....] The zero or more space patterns help prevent circumnavigating an exact match (which would probably break the link anyway, but you know users . . . )

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members