eed help cleaning strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

eed help cleaning strings

Post by pedrotuga »

Ok, i promise i will learn REGEX soon.

I need to clean strings so only A-Z a-z and 0-9 stay on the string, the remain characters will be replaced by spaces or underscores.

is there anyway of doing this using regex or should i loop the string character by character and check if it«s valid?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Of course there's a way to do it with regex. Try something first and post it. We'll tell you where you've gone wrong or suggest changes accordingly. Or you could just look through all the threads in the Regex forum.
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

Ok... it was easier than i expected, i didnt know with function to use... i searched for "replace" on php-net.

Code: Select all

<?php
$string = ereg_replace("[^A-Za-z0-9]", " ", $string);
?>
any issue on that line of code?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Other than using ereg instead of preg, nope.
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

feyd wrote:Other than using ereg instead of preg, nope.
in practical terms what would the diference be in this case?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Performance and future compatibility.
Post Reply