Forum Moderators: coopster

Message Too Old, No Replies

Stripping Spaces From A User Inputted String

         

mhoctober

10:56 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



People...

Would you be able to give me a heads up on the functions available in php to

"step through a string and copy characters to a temporary string ONLY if they are NOT a space."

Reasons : I have a registration form where a user enters their tel number.

To locate their record later either the same user or different users enter the telephone number in a search form.

Users could be potentially anywhere is the world and I'm concerned that some will use spaces - others wont so..

A. when the initial user first keys their phone number I want to store it with NO spaces at all. Then when..

B. The search facility is being used by anyone - again I want to strip all spaces for the number entered and located the 'space less' phone number in the database.

Many thanks

mooger35

11:02 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



str_replace would be a good bet.

mooger35

11:13 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



as an example with removing "-", "(" and ")"

<?php
$myArray = array(
' ' => '',
'(' => '',
')' => '',
'-' => '',
);
$phone = "(555) 555-5555";
echo str_replace(array_keys($myArray), array_values($myArray), $phone);
?>

will give you 5555555555

jezra

11:34 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



I would use preg_replace
example:
$phone_number = "1 (800) 555 1212 extension 12345";
// "/\D/" will match all characters that are NOT digits
$trimmed_number = preg_replace("/\D/","",$phone_number);