View Single Post
Old 10-19-2007, 07:17 PM   #1 (permalink)
Andrew
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default Cleaning out unwanted characters *solved*

I think it was Salathe or someone that gave me a function a while ago I could use that did this, but I lost it, and would also like to code my own.

I'm just creating a simple domain checker script, and if a user inputs a domain with characters not allowed (!@#$%$^&*(), etc.) I want to delete those from the query, and not just output an error message for the user to change his input.

First of all, I was trying to use preg_match() with a regex string, but then I was stuck on how to delete the unwanted characters to make it match. I then was going to use str_split() to do individual characters and then run it through a while or foreach loop, and whenever a character matches a certain character (for example, while(preg_match("[a-zA-Z0-9\-]", $letterArray))), however there is one major problem.

If I do that, I will have to weed out every single non-alphanumeric character (besides hyphens), except one period. I could explode() the domain and then separate the actual name from the extension, but what if the user accidentally put more than one period? That wouldn't work.

So here is my plan, and I'm not sure if anyone can help me come up with a better one.
1. Preg_match() to see if it follows the format of xxxxx.xxx (will allow special characters)
2. If it doesn't, just spit out an error or do some fancy coding to make it work
3. Then go through each character and delete the ones that aren't allowed.

Any ideas? I've been working on this for an hour to no avail.

Last edited by Andrew : 10-20-2007 at 12:09 AM.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote