regex for multiple line breakes

regex for multiple line breakes

am 14.10.2009 12:17:12 von Merlin Morgenstern

Hi there,

I am trying to remove multiple linebreakes from a textarea input.
Spammers tend to insert multiple line breakes. The problem is, that I
want to allow 2 line breaks so basic formating should be allowed.

I am doing this by regex:
$data[txt] = preg_replace('`[\r\n]+`',"\n",$data[txt]);

I would need a regex that allows \r\n\r\n, but not more than this.

Thank you for any help,

Merlin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: regex for multiple line breakes

am 14.10.2009 12:26:40 von Fernando Castillo Aparicio

--0-918964605-1255516000=:82590
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

You are replacing 1 or more matchs of a new line. To match 2 or more you ca=
n use "{2,}". It's a range, first number means min matches, second max matc=
hes. Omitting last number means no max limit. $data[txt] =3D preg_repl=
ace('`[\r\n]{2,}`',"\n",$data[txt]); ____________ _______________=
_____=0ADe: Merlin Morgenstern =0APara: php-general@l=
ists.php.net=0AEnviado: mi=E9,14 octubre, 2009 12:17=0AAsunto: [PHP] regex =
for multiple line breakes Hi there, I am trying to remove multipl=
e linebreakes from a textarea input. Spammers tend to insert multiple line =
breakes. The problem is, that I want to allow 2 line breaks so basic format=
ing should be allowed. I am doing this by regex:=0A$data[txt] =3D preg=
_replace('`[\r\n]+`',"\n",$data[txt]); I would need a regex that allow=
s \r\n\r\n, but not more than this. Thank you for any help, Merli=
n -- PHP General Mailing List (http://www.php.net/)=0ATo unsubscribe, =
visit: http://www.php.net/unsub.php =0A
--0-918964605-1255516000=:82590--

Re: regex for multiple line breakes

am 14.10.2009 12:42:23 von Merlin Morgenstern

That sounds very logical but does not work unfortunatelly.
The result is the same. It removes all linebreakes but one.
I would like to pass this one:

---------
first line

second
third
---------

But not this one:
---------
third




forth
--------



Fernando Castillo Aparicio schrieb:
> You are replacing 1 or more matchs of a new line. To match 2 or more you can use "{2,}". It's a range, first number means min matches, second max matches. Omitting last number means no max limit.
>
> $data[txt] = preg_replace('`[\r\n]{2,}`',"\n",$data[txt]);
>
>
>
> ________________________________
> De: Merlin Morgenstern
> Para: php-general@lists.php.net
> Enviado: mié,14 octubre, 2009 12:17
> Asunto: [PHP] regex for multiple line breakes
>
> Hi there,
>
> I am trying to remove multiple linebreakes from a textarea input. Spammers tend to insert multiple line breakes. The problem is, that I want to allow 2 line breaks so basic formating should be allowed.
>
> I am doing this by regex:
> $data[txt] = preg_replace('`[\r\n]+`',"\n",$data[txt]);
>
> I would need a regex that allows \r\n\r\n, but not more than this.
>
> Thank you for any help,
>
> Merlin
>
> -- PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: regex for multiple line breakes

am 14.10.2009 12:44:32 von Ashley Sheridan

--=-3P9cZTPV+UmELuov92eb
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Wed, 2009-10-14 at 12:42 +0200, Merlin Morgenstern wrote:

> That sounds very logical but does not work unfortunatelly.
> The result is the same. It removes all linebreakes but one.
> I would like to pass this one:
>=20
> ---------
> first line
>=20
> second
> third
> ---------
>=20
> But not this one:
> ---------
> third
>=20
>=20
>=20
>=20
> forth
> --------
>=20
>=20
>=20
> Fernando Castillo Aparicio schrieb:
> > You are replacing 1 or more matchs of a new line. To match 2 or more yo=
u can use "{2,}". It's a range, first number means min matches, second max =
matches. Omitting last number means no max limit.
> >=20
> > $data[txt] =3D preg_replace('`[\r\n]{2,}`',"\n",$data[txt]);
> >=20
> >=20
> >=20
> > ________________________________
> > De: Merlin Morgenstern
> > Para: php-general@lists.php.net
> > Enviado: mié,14 octubre, 2009 12:17
> > Asunto: [PHP] regex for multiple line breakes
> >=20
> > Hi there,
> >=20
> > I am trying to remove multiple linebreakes from a textarea input. Spamm=
ers tend to insert multiple line breakes. The problem is, that I want to al=
low 2 line breaks so basic formating should be allowed.
> >=20
> > I am doing this by regex:
> > $data[txt] =3D preg_replace('`[\r\n]+`',"\n",$data[txt]);
> >=20
> > I would need a regex that allows \r\n\r\n, but not more than this.
> >=20
> > Thank you for any help,
> >=20
> > Merlin
> >=20
> > -- PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >=20
> >=20
> > =20
>=20


You still have an issue with your regex:

$data[txt] =3D preg_replace('`[\r\n]+`',"\n",$data[txt]);

Even if you replace the + with a {2,} you are asking it to match any one
of the characters in the square brackets twice or more and replace it
with a single \n. If your line breaks actually do consist of the \r\n
pattern, then the {2,} will match those two characters, not two sets of
those characters. You might be better off replacing all \r characters
with an empty string, and then matching against the \n character only.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-3P9cZTPV+UmELuov92eb--

Re: regex for multiple line breakes

am 14.10.2009 13:01:54 von Fernando Castillo Aparicio

--0-2042220397-1255518114=:64819
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Right. I saw it later. To be completely multiplatform I'd use a grouping: (=
\n|\r|\r\n){2,}. If I'm not wrong, these are all the actual formats for a n=
ew line. If we replace \r with a newline we could fail if the text com=
es from a MAC OS. =0A________________________________=0ADe: Ashl=
ey Sheridan =0APara: Merlin Morgenstern @fastmail.fm>=0ACC: php-general@lists.php.net=0AEnviado: mi=E9,14 octubre, =
2009 12:44=0AAsunto: Re: [PHP] regex for multiple line breakes On Wed,=
2009-10-14 at 12:42 +0200, Merlin Morgenstern wrote: > That sounds ve=
ry logical but does not work unfortunatelly.=0A> The result is the same. It=
removes all linebreakes but one.=0A> I would like to pass this one:=0A> =
=0A> ---------=0A> first line=0A> =0A> second=0A> third=0A> ---------=0A> =
=0A> But not this one:=0A> ---------=0A> third=0A> =0A> =0A> =0A> =0A> fort=
h=0A> --------=0A> =0A> =0A> =0A> Fernando Castillo Aparicio schrieb:=0A> >=
You are replacing 1 or more matchs of a new line. To match 2 or more you c=
an use "{2,}". It's a range, first number means min matches, second max mat=
ches. Omitting last number means no max limit.=0A> > =0A> > $data[txt] =3D =
preg_replace('`[\r\n]{2,}`',"\n",$data[txt]);=0A> > =0A> > =0A> > =0A> > __=
______________________________=0A> > De: Merlin Morgenstern ail.fm>=0A> > Para: php-general@lists.php.net=0A> > Enviado: mi=E9,14 octub=
re, 2009 12:17=0A> > Asunto: [PHP] regex for multiple line breakes=0A> > =
=0A> > Hi there,=0A> > =0A> > I am trying to remove multiple linebreakes fr=
om a textarea input. Spammers tend to insert multiple line breakes. The pro=
blem is, that I want to allow 2 line breaks so basic formating should be al=
lowed.=0A> > =0A> > I am doing this by regex:=0A> > $data[txt] =3D preg_rep=
lace('`[\r\n]+`',"\n",$data[txt]);=0A> > =0A> > I would need a regex that a=
llows \r\n\r\n, but not more than this.=0A> > =0A> > Thank you for any help=
,=0A> > =0A> > Merlin=0A> > =0A> > -- PHP General Mailing List (http://www.=
php.net/)=0A> > To unsubscribe, visit: http://www.php.net/unsub.php=0A> > =
=0A> > =0A> > =0A> =0AYou still have an issue with your regex:=
$data[txt] =3D preg_replace('`[\r\n]+`',"\n",$data[txt]); Even i=
f you replace the + with a {2,} you are asking it to match any one=0Aof the=
characters in the square brackets twice or more and replace it=0Awith a si=
ngle \n. If your line breaks actually do consist of the \r\n=0Apattern, the=
n the {2,} will match those two characters, not two sets of=0Athose charact=
ers. You might be better off replacing all \r characters=0Awith an empty st=
ring, and then matching against the \n character only. Thanks,=0AAsh=
=0Ahttp://www.ashleysheridan.co.uk =0A
--0-2042220397-1255518114=:64819--

Re: regex for multiple line breakes

am 14.10.2009 13:14:27 von Merlin Morgenstern

Ashley Sheridan schrieb:
> On Wed, 2009-10-14 at 12:42 +0200, Merlin Morgenstern wrote:
>
>> That sounds very logical but does not work unfortunatelly.
>> The result is the same. It removes all linebreakes but one.
>> I would like to pass this one:
>>
>> ---------
>> first line
>>
>> second
>> third
>> ---------
>>
>> But not this one:
>> ---------
>> third
>>
>>
>>
>>
>> forth
>> --------
>>
>>
>>
>> Fernando Castillo Aparicio schrieb:
>>> You are replacing 1 or more matchs of a new line. To match 2 or more you can use "{2,}". It's a range, first number means min matches, second max matches. Omitting last number means no max limit.
>>>
>>> $data[txt] = preg_replace('`[\r\n]{2,}`',"\n",$data[txt]);
>>>
>>>
>>>
>>> ________________________________
>>> De: Merlin Morgenstern
>>> Para: php-general@lists.php.net
>>> Enviado: mié,14 octubre, 2009 12:17
>>> Asunto: [PHP] regex for multiple line breakes
>>>
>>> Hi there,
>>>
>>> I am trying to remove multiple linebreakes from a textarea input. Spammers tend to insert multiple line breakes. The problem is, that I want to allow 2 line breaks so basic formating should be allowed.
>>>
>>> I am doing this by regex:
>>> $data[txt] = preg_replace('`[\r\n]+`',"\n",$data[txt]);
>>>
>>> I would need a regex that allows \r\n\r\n, but not more than this.
>>>
>>> Thank you for any help,
>>>
>>> Merlin
>>>
>>> -- PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>>
>
>
> You still have an issue with your regex:
>
> $data[txt] = preg_replace('`[\r\n]+`',"\n",$data[txt]);
>
> Even if you replace the + with a {2,} you are asking it to match any one
> of the characters in the square brackets twice or more and replace it
> with a single \n. If your line breaks actually do consist of the \r\n
> pattern, then the {2,} will match those two characters, not two sets of
> those characters. You might be better off replacing all \r characters
> with an empty string, and then matching against the \n character only.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
Thank you. That worked!
$data[txt] = preg_replace('`[\n]{3,}`',"\n",str_replace("\r",
"",$data[txt]));

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php