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($formdata)
{
foreach ($formdata as $key => $value)
{
$value = preg_replace("(\r\n|\n|\r|\t)", "", $value);
$newdata[trim($key)] = trim($value);
}
return $newdata;
} | | | | 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(). | | | | 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 | | | | 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? | | | | 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 | | | | 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!! |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|