Page 1 of 1

eed help cleaning strings

Posted: Wed Nov 15, 2006 9:06 pm
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?

Posted: Wed Nov 15, 2006 9:10 pm
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.

Posted: Wed Nov 15, 2006 9:41 pm
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?

Posted: Wed Nov 15, 2006 9:46 pm
by feyd
Other than using ereg instead of preg, nope.

Posted: Wed Nov 15, 2006 9:49 pm
by pedrotuga
feyd wrote:Other than using ereg instead of preg, nope.
in practical terms what would the diference be in this case?

Posted: Wed Nov 15, 2006 9:50 pm
by feyd
Performance and future compatibility.