thewird Posted August 15, 2006 Share Posted August 15, 2006 I was wondering how you check a URL in a variable to see that it is in proper format. What I mean by this is that it has no illegal characters for URL and can exist but doesn't mean it necessarily exists (don't need to check if it actually exists on the web). If anyone could help me with this, that would be great. Thanks.thewird Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 15, 2006 Share Posted August 15, 2006 Copied from http://www.weberdev.com/get_example-4227.html[code]<?php/*The next code checks the following :o URL starts with a-z, A-Z (e.g. http, ftp...)o It is then followed by ://o next we have more letters, numbers and special chars : a-z, A-Z, 0-9, -, _o there must be at least one "."o A URL can contain at it's end part the more special characters and this iswhy the last part allows for more options such as ?, /, &, %, etc...you may add/remove chars as needed to your code.*/// Assuming that the URL is received from a FORM by POST with the field name being "URL" the code// Looks like this :if(!preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i",$_POST[URL])) {Echo"You must supply a valid URL.";Exit();}?>[/code] Quote Link to comment Share on other sites More sharing options...
thewird Posted August 15, 2006 Author Share Posted August 15, 2006 Thank you very much. It works great :).thewird Quote Link to comment Share on other sites More sharing options...
thewird Posted August 15, 2006 Author Share Posted August 15, 2006 Oh I just realized it allows for any urls. What I was really looking for and wasn't quite clear was for domain name only, meaning something.extension (ie domain.com)For the preg_match part i did...preg_match("/^[A-Za-z0-9\-_]+\\.+[A-Za-z0-9-_]+$/i",$domainname)However, I'm not sure what +$/i does. Could anyone help? THanks.thewird Quote Link to comment Share on other sites More sharing options...
thewird Posted August 15, 2006 Author Share Posted August 15, 2006 Anyone?thewird Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 15, 2006 Share Posted August 15, 2006 Your getting a head start hear but relly you need to read the manual on regular exspestionsgood luck//if the format of the url is a www.xxxxxx.com[code]<?php$url="www.phpfreaks.com";if(!eregi("^[a-zA_Z]{3}.[a-zA-Z]{1,100}\.[a-zA-Z]{3}",$url)){echo "incorrect web address";}else{echo "correct web address";}?>[/code]//if the format of the url is a http://www.xxxxxx.com[code]<?php$url="http://www.phpfreaks.com";if(!eregi("^(http://)[a-zA_Z]{3}.[a-zA-Z]{1,100}\.[a-zA-Z]{3}",$url)){echo "incorrect web address";}else{echo "correct web address";}?>[/code]//if the format of the url is a www.xxxxxx.com but only a .com[code]<?php$url="www.phpfreaks.com";if(!eregi("^[a-zA_Z]{3}.[a-zA-Z]{1,100}\.(com)",$url)){echo "incorrect web address";}else{echo "correct web address";}?>[/code]//if the format of the url is a www.xxxxxx.com but only a .com or .net[code]<?php$url="www.phpfreaks.net";if(!eregi("^[a-zA_Z]{3}.[a-zA-Z]{1,100}\.(com|net)",$url)){echo "incorrect web address";}else{echo "correct web address";}?>[/code] Quote Link to comment Share on other sites More sharing options...
SharkBait Posted August 15, 2006 Share Posted August 15, 2006 $/i is for case-insensative I believe. Quote Link to comment Share on other sites More sharing options...
thewird Posted August 15, 2006 Author Share Posted August 15, 2006 [quote author=SharkBait link=topic=104325.msg416292#msg416292 date=1155660310]$/i is for case-insensative I believe.[/quote]Ok thanks. I think the code I posted above should do the trick then.thewird Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.