homepage Welcome to WebmasterWorld Guest from 54.196.62.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
validate form help
need to check how value starts
weddingm

5+ Year Member



 
Msg#: 4018003 posted 7:11 am on Nov 3, 2009 (gmt 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

 

Tommybs

5+ Year Member



 
Msg#: 4018003 posted 7:40 am on Nov 3, 2009 (gmt 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{

}

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4018003 posted 9:50 am on Nov 3, 2009 (gmt 0)

@ 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 */ }

weddingm

5+ Year Member



 
Msg#: 4018003 posted 6:23 pm on Nov 3, 2009 (gmt 0)

Thanks, I like the idea of just taking it out. Will the replace code above remove both HTTP:// and http:// ?

Tommybs

5+ Year Member



 
Msg#: 4018003 posted 6:27 pm on Nov 3, 2009 (gmt 0)

try using str_replace("http://","",strtolower($submittedURL)); that should be case-insensitive now.

weddingm

5+ Year Member



 
Msg#: 4018003 posted 6:31 pm on Nov 3, 2009 (gmt 0)

thanks!

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4018003 posted 3:06 am on Nov 4, 2009 (gmt 0)

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

JohnCanyon

10+ Year Member



 
Msg#: 4018003 posted 10:29 pm on Nov 4, 2009 (gmt 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

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4018003 posted 3:43 am on Nov 5, 2009 (gmt 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 . . . )

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved