Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 105,772 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,473 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Strip characters from a phone number using trim()

 
Reply to this topicStart new topic

Strip characters from a phone number using trim()

bstonehill
post 24 Jul, 2008 - 01:02 PM
Post #1


New D.I.C Head

Group Icon
Joined: 21 Jan, 2008
Posts: 26



Thanked 1 times
My Contributions


I'm trying to strip the hyphens out of a posted phone number field so that I can re-format them uniformly whether the user uses hyphens or not. I've used trim() before without much trouble but for some reason it's not stripping out the characters.

CODE

    function formatPhone($ph){
        $trimmed = trim($ph, "-");
        $phA = substr($trimmed, 0, 3);
        $phB = substr($trimmed, 3, 3);
        $phC = substr($trimmed, 6);
        return $phA."-".$phB."-".$phC;
    }
    
    $phone = formatPhone($_POST['phone']);
    $contact_phone = formatPhone($_POST['contact_phone']);
User is offlineProfile CardPM

Go to the top of the page


Moonbat
post 24 Jul, 2008 - 01:29 PM
Post #2


D.I.C Head

Group Icon
Joined: 30 Jun, 2008
Posts: 100



Thanked 6 times

Dream Kudos: 400
My Contributions


Why don't you just use str_replace and do something like:

CODE
str_replace("-", "", $thephonenumber

It'll replace the hyphen with nothing, in other words, it'll get rid of the hyphens.

This post has been edited by Moonbat: 24 Jul, 2008 - 01:30 PM
User is online!Profile CardPM

Go to the top of the page

bstonehill
post 24 Jul, 2008 - 02:06 PM
Post #3


New D.I.C Head

Group Icon
Joined: 21 Jan, 2008
Posts: 26



Thanked 1 times
My Contributions


QUOTE(Moonbat @ 24 Jul, 2008 - 04:29 PM) *

Why don't you just use str_replace and do something like:

CODE
str_replace("-", "", $thephonenumber

It'll replace the hyphen with nothing, in other words, it'll get rid of the hyphens.


That did the trick, thanks!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 03:29PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month