ereg_replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    ereg_replace

    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



  • Krustov

    #2
    Re: ereg_replace

    <comp.lang.ph p>
    <Bob Bedford>
    <Wed, 5 Jul 2006 16:38:56 +0200>
    <44abcefc$0$126 18$5402220f@new s.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( 'ABCDEFGHIJKLMN OPQRSTUZWXYZabc defghijklmnopqr stuvwxyz_
    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,$allow ed)
    {
    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

    Comment

    • David Haynes

      #3
      Re: ereg_replace

      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-

      Comment

      • Bob Bedford

        #4
        Re: ereg_replace


        "Krustov" <me@privacy.net a écrit dans le message de news:
        MPG.1f15e57e76d 40b9989b4f@news .newsreader.com...
        <comp.lang.ph p>
        <Bob Bedford>
        <Wed, 5 Jul 2006 16:38:56 +0200>
        <44abcefc$0$126 18$5402220f@new s.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( 'ABCDEFGHIJKLMN OPQRSTUZWXYZabc defghijklmnopqr stuvwxyz_
        >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,$allow ed)
        {
        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

        >
        thanks for your code: I've been able to do as simple as:
        $number = ereg_replace("[^0-9/.]",".",$numb er);
        (we decided to remove the "space" but may be easely be added I think).

        Bob



        Comment

        • xicheng

          #5
          Re: ereg_replace

          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

          Comment

          • Krustov

            #6
            Re: ereg_replace

            <comp.lang.ph p>
            <Bob Bedford>
            <Wed, 5 Jul 2006 17:57:51 +0200>
            <44abe17c$0$126 15$5402220f@new s.sunrise.ch>
            thanks for your code: I've been able to do as simple as:
            $number = ereg_replace("[^0-9/.]",".",$numb er);
            (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

            Comment

            • Krustov

              #7
              Re: ereg_replace (loop)

              <comp.lang.ph p>
              <Krustov>
              <Wed, 5 Jul 2006 17:08:24 +0100>
              <MPG.1f15f4355f 47a240989b50@ne ws.newsreader.c om>
              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.ph p



              <?php

              $ok="0123456789/.";

              $clown=strlen($ ok); $clown=$clown-1;
              $lisa=0;
              while($lisa<$cl own+1)
              {
              $char=substr($o k,$lisa,1);
              $millhouse[$lisa]=$char;
              $lisa=$lisa+1;
              }

              $krusty=strlen( $num); $krusty=$krusty-1;
              $lisa=0;
              while($lisa<$kr usty+1)
              {
              $char=substr($n um,$lisa,1);
              $jimbo[$lisa]=$char;
              $lisa=$lisa+1;
              }

              $lisa=0; $fin="";
              while($lisa<$kr usty+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

              Comment

              Working...