Function to strip returns

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

    Function to strip returns

    I'm trying to write a function that will trim and strip new line
    character returns from submitted $_POST values. Does this look serviceable?

    function trim_data($form data)
    {
    foreach ($formdata as $key => $value)
    {
    $value = preg_replace("( \r\n|\n|\r|\t)" , "", $value);
    $newdata[trim($key)] = trim($value);

    }
    return $newdata;
    }
  • ZeldorBlat

    #2
    Re: Function to strip returns

    Sure, or just use:

    $value = str_replace(arr ay("\r", "\n", "\t"), '', $value);

    It's probably a little bit faster than preg_replace().

    Comment

    • Ewoud Dronkert

      #3
      Re: Function to strip returns

      JackM wrote:[color=blue]
      > $value = preg_replace("( \r\n|\n|\r|\t)" , "", $value);[/color]

      That is not a pattern, it needs pattern delimiters at the start and end.
      You don't need the parentheses because there's nothing outside them. Use a
      quantifier to match series of newlines/tabs. Use a character class to
      match different combinations of characters. I always use single quotes
      around patterns to minimize the use of backslashes.

      $value = preg_replace('/[\r\n\t]+/', '', $value);

      --
      E. Dronkert

      Comment

      • JackM

        #4
        Re: Function to strip returns

        JackM wrote:
        [color=blue]
        > I'm trying to write a function that will trim and strip new line
        > character returns from submitted $_POST values. Does this look serviceable?
        >
        > function trim_data($form data)
        > {
        > foreach ($formdata as $key => $value)
        > {
        > $value = preg_replace("( \r\n|\n|\r|\t)" , "", $value);
        > $newdata[trim($key)] = trim($value);
        >
        > }
        > return $newdata;
        > }[/color]

        ZeldorBlat wrote:
        [color=blue][color=green]
        >> $value = str_replace(arr ay("\r", "\n", "\t"), '', $value);[/color][/color]

        Ewoud Dronkert wrote:
        [color=blue]
        > $value = preg_replace('/[\r\n\t]+/', '', $value);[/color]


        Interesting. So what would the preferred method be or is that just
        splitting hairs?

        Comment

        • Ewoud Dronkert

          #5
          Re: Function to strip returns

          JackM wrote:[color=blue]
          > ZeldorBlat wrote:
          >[color=green][color=darkred]
          > >> $value = str_replace(arr ay("\r", "\n", "\t"), '', $value);[/color][/color]
          >
          > Ewoud Dronkert wrote:
          >[color=green]
          > > $value = preg_replace('/[\r\n\t]+/', '', $value);[/color]
          >
          >
          > Interesting. So what would the preferred method be or is that just
          > splitting hairs?[/color]

          In principle, I say prefer str_replace. Maybe if $value contained lots of
          consecutive \r, \n or \t, the regexp might even be a little faster?
          Actually I didn't think about the possibility to use an array argument for
          str_replace so just used a regexp, with which I'm quite comfortable.

          --
          E. Dronkert

          Comment

          • Adam i Agnieszka Gasiorowski FNORD

            #6
            Re: Function to strip returns

            On 2005-10-31 08:41:59 +0000, Ewoud Dronkert
            <firstname@last name.net.invali d> said:
            [color=blue]
            > JackM wrote:[color=green]
            >> $value = preg_replace("( \r\n|\n|\r|\t)" , "", $value);[/color]
            >
            > That is not a pattern, it needs pattern delimiters at the start and end.
            > You don't need the parentheses because there's nothing outside them. Use a
            > quantifier to match series of newlines/tabs. Use a character class to
            > match different combinations of characters. I always use single quotes
            > around patterns to minimize the use of backslashes.
            >
            > $value = preg_replace('/[\r\n\t]+/', '', $value);[/color]



            You are right about the delimiters, but
            wrong about the pattern. I did not test
            it, but using a greedy quantifier instead
            of alternative is not a good idea. I would
            write it as a non capturing (?:) and put
            the most probable alternative first, which
            is, correctly, \r\n (Windows), the rest of
            them don't matter that much as in almost 9/10
            PCRE engine will end matching at the first
            branch (it short-circuits) ---- Windows clients
            are 90%+ of all accessing web pages. Feel free to
            correct me if I'm wrong.

            -- 
            I am the One. I am A vampire A-calling for your love! A.A!
            I am the fire that burns within your blood. I am the One!!
            No bars or chains can keep me from your bed! I am the One!
            Nothing on earth can get me from your head! I am the One!!

            Comment

            Working...