Announcement

Collapse
No announcement yet.

Stripping characters/whitespace using PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Stripping characters/whitespace using PHP

    I'm attempting a bit of php and wondered if someone could point me in the right direction.

    Code:
    $return = trim(ereg_replace(' +',' ',preg_replace('/[^a-zA-Z0-9.\s]/','',strtolower($input))));

    This transforms to lowercase, removes punctuation with the exception of '.' and removes multiple/leading/ending spaces.

    I was wondering if it is possible to leave '-' IF it is in a number e.g. 1800-2?

    Any help greatfully received.

    #2
    What does the input look like?

    Comment


      #3
      Your input is not really a number so you would need to check the preceding character before a - to check if it was a number.

      You can of course also simply exclude - from the function which may help...

      Code:
      $return = trim(ereg_replace(' +',' ',preg_replace('/[^a-zA-Z0-9.\s-\s]/','',strtolower($input))));

      Comment


        #4
        One way would be to protect the number-range occurences before applying the main strip, then reversing the procedure:
        I've replaced the '-' with a 'z' to do this

        PHP Code:
        $input ereg_replace('/([0-9]+)-([0-9]+)/''/$1z$2/'$input);
        $input trim(ereg_replace(' +'' 'preg_replace('/[^a-zA-Z0-9.\s]/'''strtolower($input))));
        $return ereg_replace('/([0-9]+)z([0-9]+)/''/$1-$2/'$input); 

        Comment


          #5
          I'm no php guru but I couldn't get Alan's code to give the right result but this did (perhaps they should both work):

          PHP Code:
          $input ereg_replace('([0-9]+)-([0-9]+)''\\1zzzz\\2'$input);
          $input trim(ereg_replace(' +'' 'preg_replace('/[^a-zA-Z0-9.\s]/'''strtolower($input))));
          $return ereg_replace('([0-9]+)zzzz([0-9]+)''\\1-\\2'$input); 
          I changed the z to zzzz just in case you have a z in the input string - chances are you'll never have zzzz though.

          Comment


            #6
            Originally posted by drounding View Post
            I'm no php guru but I couldn't get Alan's code to give the right result
            Ooops, I tested using the .NET regex class rather than PHP. I glibly assumed the two implementations of regular expression should be the same.

            Comment


              #7
              Here's a very lightweight standalone PHP test environment that I find very useful:
              http://www.zachsaw.co.cc/?pg=quickph...ester_debugger

              Comment


                #8
                * high-fives Duncan *

                Comment


                  #9
                  Originally posted by drounding View Post
                  Here's a very lightweight standalone PHP test environment that I find very useful:
                  http://www.zachsaw.co.cc/?pg=quickph...ester_debugger
                  for me that has to be one of the best links this year, thanks Duncan

                  Malcolm

                  SellerDeck Accredited Partner,
                  SellerDeck 2016 Extensions, and
                  Custom Packages

                  Comment


                    #10
                    Thank you to you both. Just the job!

                    Comment

                    Working...
                    X