note 73801 deleted from function.preg-replace by nlopess

From: Date: Sun, 11 Mar 2007 12:59:46 +0000
Subject: note 73801 deleted from function.preg-replace by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-124340@lists.php.net to get a copy of this message
Note Submitter: neillindberg ---- This is rudimentary for pretty much any coder posting before me, but they didn't answer my question... So, I wanted to strip phone numbers of all but digits (there is no length test so this is an incomplete process before writing to database or displaying proper number). // //three variables as pretend phone strings $one = "555.123.1234"; $two = "555-123.1234"; $three = " &nbsp; (555)-123-1234abc"; echo "<h3>before</h3><p>$one<br />$two<br />$three</p>"; // //strips, trims, replaces non-digit with nothing. // function formatPhone($phoneString){ $phoneString = stripslashes(trim($phoneString)); $patterns = '[\D]'; // \D matches any non-digit character. $replacement = ''; //empty single quotes return preg_replace($patterns, $replacement, $phoneString); } // $one = formatPhone($one); $two = formatPhone($two); $three = formatPhone($three); echo "<h3>after</h3><p>$one<br />$two<br />$three</p>"; // //The above function could be smaller. /*Looks like: before 555.123.1234 555-123.1234 (555)-123-1234abc after 5551231234 5551231234 5551231234 Hope this helps a regex beginner like me. */

« previous php.notes (#124340) next »