note 73800 added to function.preg-replace
| From: | php-general at lists dot php dot net | Date: | Sun, 11 Mar 2007 01:12:05 +0000 |
| Subject: | note 73800 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124311@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 = " (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/73800
Del: integrated -- https://master.php.net/note/delete/73800/integrated
Del: useless -- https://master.php.net/note/delete/73800/useless
Del: bad code -- https://master.php.net/note/delete/73800/bad+code
Del: spam -- https://master.php.net/note/delete/73800/spam
Del: non-english -- https://master.php.net/note/delete/73800/non-english
Del: in docs -- https://master.php.net/note/delete/73800/in+docs
Del: other reasons-- https://master.php.net/note/delete/73800
Reject -- https://master.php.net/note/reject/73800
Search -- https://master.php.net/manage/user-notes.php