TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-19-2007, 07:17 PM   #1 (permalink)
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
Old 10-19-2007, 07:42 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Anything that can be preg_matched can also be preg_replaced, so you might be able to filter out disallowed characters (depending on the specific use) in one line of code.
Salathe is offline  
Reply With Quote
Old 10-19-2007, 07:57 PM   #3 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Well, I thought of that, but I'd have to type in every single special character that can be inputted to make it truly safe, and I'd miss a lot, like the Alt-code ones.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 10-19-2007, 10:08 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

With regular expressions you can match characters which aren't something. For example [^0-9] would match anything which isn't a single digit.
Salathe is offline  
Reply With Quote
Old 10-19-2007, 10:33 PM   #5 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

But how can I use preg_match() to clear out all the unwanted characters using regular expressions? For example, if I used:
PHP Code:
<?php
$string 
"!@#$abc";
echo 
preg_match("/^abc/"$string);
?>
That will output "abc", but what happens if I have for example "a!@#b!@#c", and try to use that same pattern? It wouldn't show any matches. So how could I do it using just preg_match?
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 10-20-2007, 04:07 PM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

use preg_replace() instead of preg_match()
example:
PHP Code:
<?php
$string 
'cheesedwawo24mlkdj;ao8';
echo 
preg_replace("/^cheese/",""$string);
?>
will return "dwawo24mlkdj;ao8"

hope that helps :)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 10-20-2007, 08:31 PM   #7 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Yeah, thanks. I completely forgot how the output worked on preg_match(). Sigh, I have quite a way to go to get back to how I used to be with PHP.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:38 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design