DavidGS Posted December 24, 2007 Share Posted December 24, 2007 You know how when you register a MySpace account you can choose a personal URL? Basically I am making a social networking website and I am adding the same feature. The only problem is how would I validate a URL the person registering has entered to make sure it doesn't contain any characters that could case problems? Like, I wouldn't want the person entering @ because that would be interpreted as an e-mail address or . because that would be mistaken for a file type separator. I want to validate it to make sure it only contains letters, numbers and underscores. Anyone have an expression I could use? Quote Link to comment Share on other sites More sharing options...
Orio Posted December 24, 2007 Share Posted December 24, 2007 What exactly do you want to have in this input? If it's simply "only letters number and underscores" you'd do something like this: if(preg_match("/[a-z0-9_]+/i", $input)) ... But if you want to check if the input is a valid myspace url, you'll have to do something more specific than that. Define exactly what is valid and what is not. Give a few examples too. I once needed to validate if an inputted url was a valid myspace profile url. I don't know if that's what you are looking for. But if it is, this is what I did (first expression deals with urls that have nicknames, the second one deals with those that have the ID in them): $regex1 = "|^(http:\/\/){0,1}(www\.){0,1}myspace\.com\/[a-z0-9_]+$|i"; $regex2 = "|^(http:\/\/){0,1}profile\.myspace\.com\/index\.cfm\?fuseaction=user\.viewprofile&friendID=[0-9]+$|i"; Orio. Quote Link to comment Share on other sites More sharing options...
DavidGS Posted December 24, 2007 Author Share Posted December 24, 2007 Orio, Basically the user can put in anything they like as long as it doesn't have certain characters in it. Example, they register with the URL "joe", their personal URL would be http://www.website.com/joe/ Because this will be an actual URL I don't want characters than can cause any errors when trying to access it. So by limiting what the user can select as his personal URL I can avoid any errors The user will be able to enter letters, numbers and underscores and them only. They register with the URL "joe_bob6", their personal URL would be http://www.website.com/joe_bob6/ That would be an acceptable URL and there would be no issue accessing it. But if the user registered with the URL "$joe%bob/", their URL would be http://www.website.com/$joe%bob// which may be problematic and cause an interpretation error in PHP (particularly with the dollar sign). This is so that can be avoided. Quote Link to comment Share on other sites More sharing options...
DavidGS Posted December 25, 2007 Author Share Posted December 25, 2007 Doesn't anybody know? 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.