This is a discussion on ereg_replace within the PHP Language forums, part of the PHP Programming Forums category; Hello there, I need help: I've a string that is used to write down a phone number that comes ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello there,
I need help: I've a string that is used to write down a phone number that comes from a form and would like to keep it as clean as possible. For avoiding people to create twice an account using a different syntax. For this the only char I'd like to allow are number 0-9, '/','.' and spaces. I'd like to replace everything other by a "space". Lets give some examples: some allowed: 555/55.55.55 555 55 55 55 555.555 555 some not allowed and their changes: 555-55-55-55 -> 555.55.55.55 555,55,55,55 -> 555.55.55.55 555:55;55_55 -> 555.55.55.55 55555\55>55 -> 55555.55.55 and so on. How can I do that ? I'm not confortable with ereg_replace, so if you could give the final statement it would be greately appreciated. Bob |
|
|||
|
<comp.lang.php>
<Bob Bedford> <Wed, 5 Jul 2006 16:38:56 +0200> <44abcefc$0$12618$5402220f@news.sunrise.ch> > I need help: I've a string that is used to write down a phone number that > comes from a form and would like to keep it as clean as possible. For > avoiding people to create twice an account using a different syntax. > > For this the only char I'd like to allow are number 0-9, '/','.' and spaces. > > I'd like to replace everything other by a "space". > > Lets give some examples: > > some allowed: > 555/55.55.55 > 555 55 55 55 > 555.555 555 > > some not allowed and their changes: > 555-55-55-55 -> 555.55.55.55 > 555,55,55,55 -> 555.55.55.55 > 555:55;55_55 -> 555.55.55.55 > 55555\55>55 -> 55555.55.55 > and so on. > > How can I do that ? I'm not confortable with ereg_replace, so if you could > give the final statement it would be greately appreciated. > The following is a cut-n-paste from another newsgroup ..... > if (string_reject('ABCDEFGHIJKLMNOPQRSTUZWXYZabcdefgh ijklmnopqrstuvwxyz_ > 0123',$skank)) {$pass=0;} > > Something simple like the above would be so much easier in some cases . > > Stuff like [^ makes it hard to understand . $checkthis="555:55;55_55"; $ok="0123456789\."; string_reject($checkthis,$ok) function string_reject($mystring,$allowed) { return ereg("[^".$allowed."]",$mystring); } Perhaps sombody on here could alter/add the appropriate code to do what you require it to do . -- Encrypted email address www.emailuser.co.uk/?name=KRUSTOV |
|
|||
|
Bob Bedford wrote:
> Hello there, > > I need help: I've a string that is used to write down a phone number that > comes from a form and would like to keep it as clean as possible. For > avoiding people to create twice an account using a different syntax. > > For this the only char I'd like to allow are number 0-9, '/','.' and spaces. > > I'd like to replace everything other by a "space". preg not ereg but it can easily do what you want. I got this from the PHP manual comments (I think) /** * Validates and reformats (if necessary) a North American telephone * number. Telephone number is reformatted to the following format: * 123-123-1234 x12345 * * @param string $phone * @return string or FALSE */ function checkTelephone($phone) { $na_fmt ="/^(?:\+?1[\-\s]?)?(\(\d{3}\)|\d{3})[\-\s\.]?"; //area code $na_fmt.="(\d{3})[\-\.]?(\d{4})"; // seven digits $na_fmt.="(?:\s?x|\s|\s?ext(?:\.|\s)?)?(\d*)?$/"; // any extension if( ! preg_match($na_fmt, $phone ,$match) ) { return false; } else { $ret = ''; if( substr($match[1], 0, 1 ) == '(') { $ret .= $match[1]; } else { $ret .= $match[1].'-'; } $ret .= $match[2].'-'.$match[3]; if ($match[4] != '') { $ret.=' x'.$match[4]; } return $ret; } } -david- |
|
|||
|
"Krustov" <me@privacy.net> a écrit dans le message de news: MPG.1f15e57e76d40b9989b4f@news.newsreader.com... > <comp.lang.php> > <Bob Bedford> > <Wed, 5 Jul 2006 16:38:56 +0200> > <44abcefc$0$12618$5402220f@news.sunrise.ch> > >> I need help: I've a string that is used to write down a phone number that >> comes from a form and would like to keep it as clean as possible. For >> avoiding people to create twice an account using a different syntax. >> >> For this the only char I'd like to allow are number 0-9, '/','.' and >> spaces. >> >> I'd like to replace everything other by a "space". >> >> Lets give some examples: >> >> some allowed: >> 555/55.55.55 >> 555 55 55 55 >> 555.555 555 >> >> some not allowed and their changes: >> 555-55-55-55 -> 555.55.55.55 >> 555,55,55,55 -> 555.55.55.55 >> 555:55;55_55 -> 555.55.55.55 >> 55555\55>55 -> 55555.55.55 >> and so on. >> >> How can I do that ? I'm not confortable with ereg_replace, so if you >> could >> give the final statement it would be greately appreciated. >> > > The following is a cut-n-paste from another newsgroup ..... > >> if (string_reject('ABCDEFGHIJKLMNOPQRSTUZWXYZabcdefgh ijklmnopqrstuvwxyz_ >> 0123',$skank)) {$pass=0;} >> >> Something simple like the above would be so much easier in some cases . >> >> Stuff like [^ makes it hard to understand . > > $checkthis="555:55;55_55"; > $ok="0123456789\."; > string_reject($checkthis,$ok) > > function string_reject($mystring,$allowed) > { > return ereg("[^".$allowed."]",$mystring); > } > > Perhaps sombody on here could alter/add the appropriate code to do what > you require it to do . > > > -- > Encrypted email address > www.emailuser.co.uk/?name=KRUSTOV > thanks for your code: I've been able to do as simple as: $number = ereg_replace("[^0-9/.]",".",$number); (we decided to remove the "space" but may be easely be added I think). Bob |
|
|||
|
Bob Bedford wrote:
> Hello there, > > I need help: I've a string that is used to write down a phone number that > comes from a form and would like to keep it as clean as possible. For > avoiding people to create twice an account using a different syntax. > > For this the only char I'd like to allow are number 0-9, '/','.' and spaces. > > I'd like to replace everything other by a "space". a "space" or a "dot"?? > Lets give some examples: > > some allowed: > 555/55.55.55 > 555 55 55 55 > 555.555 555 > > some not allowed and their changes: > 555-55-55-55 -> 555.55.55.55 > 555,55,55,55 -> 555.55.55.55 > 555:55;55_55 -> 555.55.55.55 > 55555\55>55 -> 55555.55.55 > and so on. > > How can I do that ? I'm not confortable with ereg_replace, so if you could > give the final statement it would be greately appreciated. It looks to me that you want to replace all characters which are not a number, a dot a space or a slash to "dot", is this true?? then you might try: $newstr = preg_replace("/[^\d.\/ ]+/", ".", $oldstr); (untested) I added a plus in the pattern /[^\d.\/ ]+/ to replace also something like: 555:55;_@55_55 to 555.55.55.55 instead of 555.55...55.55 Xicheng |
|
|||
|
<comp.lang.php>
<Bob Bedford> <Wed, 5 Jul 2006 17:57:51 +0200> <44abe17c$0$12615$5402220f@news.sunrise.ch> > thanks for your code: I've been able to do as simple as: > $number = ereg_replace("[^0-9/.]",".",$number); > (we decided to remove the "space" but may be easely be added I think). > I havent got my head round regular expressions yet either - but it would be easy to write a small loop to replace everything that wasnt 012345678/. with a space . Can write it for you if you want . -- Encrypted email address www.emailuser.co.uk/?name=KRUSTOV |
|
|||
|
<comp.lang.php>
<Krustov> <Wed, 5 Jul 2006 17:08:24 +0100> <MPG.1f15f4355f47a240989b50@news.newsreader.com> > I havent got my head round regular expressions yet either - but it would > be easy to write a small loop to replace everything that wasnt > 012345678/. with a space . > > Can write it for you if you want . > Wrote it anyway as it could be useful to me some time in the future . Quite luddite in method but seems to work fine . <?php $num="A_555. 01/2345jack678-90/.@Z"; print "Before processing = $num <br>"; include('number_check.php'); print "After processing = $fin <br>"; ?> The above on some webpage . The following as a include file . number_check.php <?php $ok="0123456789/."; $clown=strlen($ok); $clown=$clown-1; $lisa=0; while($lisa<$clown+1) { $char=substr($ok,$lisa,1); $millhouse[$lisa]=$char; $lisa=$lisa+1; } $krusty=strlen($num); $krusty=$krusty-1; $lisa=0; while($lisa<$krusty+1) { $char=substr($num,$lisa,1); $jimbo[$lisa]=$char; $lisa=$lisa+1; } $lisa=0; $fin=""; while($lisa<$krusty+1) { $homer=0; $pass=0; while ($homer<$clown+1) { if ($jimbo[$lisa]==$millhouse[$homer]) {$pass=1;} $homer=$homer+1; } if ($pass==0) {$jimbo[$lisa]=" ";} $fin=$fin . $jimbo[$lisa]; $lisa=$lisa+1; } ?> -- Encrypted email address www.emailuser.co.uk/?name=KRUSTOV |
![]() |
| Thread Tools | |
| Display Modes | |
|
|