note 73801 added to function.preg-replace

From: Date: Sun, 11 Mar 2007 01:15:45 +0000
Subject: note 73801 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-124312@lists.php.net to get a copy of this message
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. */ ---- Server IP: 64.71.164.2 Probable Submitter: 207.62.139.27 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/73801 Del: integrated -- https://master.php.net/note/delete/73801/integrated Del: useless -- https://master.php.net/note/delete/73801/useless Del: bad code -- https://master.php.net/note/delete/73801/bad+code Del: spam -- https://master.php.net/note/delete/73801/spam Del: non-english -- https://master.php.net/note/delete/73801/non-english Del: in docs -- https://master.php.net/note/delete/73801/in+docs Del: other reasons-- https://master.php.net/note/delete/73801 Reject -- https://master.php.net/note/reject/73801 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124312) next »