Connecting Tech Pros Worldwide Forums | Help | Site Map

Function to strip returns

JackM
Guest
 
Posts: n/a
#1: Oct 31 '05
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($formdata)
{
foreach ($formdata as $key => $value)
{
$value = preg_replace("(\r\n|\n|\r|\t)", "", $value);
$newdata[trim($key)] = trim($value);

}
return $newdata;
}

ZeldorBlat
Guest
 
Posts: n/a
#2: Oct 31 '05

re: Function to strip returns


Sure, or just use:

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

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

Ewoud Dronkert
Guest
 
Posts: n/a
#3: Oct 31 '05

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
JackM
Guest
 
Posts: n/a
#4: Nov 1 '05

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($formdata)
> {
> 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(array("\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?
Ewoud Dronkert
Guest
 
Posts: n/a
#5: Nov 1 '05

re: Function to strip returns


JackM wrote:[color=blue]
> ZeldorBlat wrote:
>[color=green][color=darkred]
> >> $value = str_replace(array("\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
Adam i Agnieszka Gasiorowski FNORD
Guest
 
Posts: n/a
#6: Nov 9 '05

re: Function to strip returns


On 2005-10-31 08:41:59 +0000, Ewoud Dronkert
<firstname@lastname.net.invalid> 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!!

Closed Thread