Re: [PHP] regex for multiple line breakes
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
Re: [PHP] regex for multiple line breakes
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 new line. If we replace \r with a newline we could fail if the text comes from a MAC OS. De: Ashley Sheridan Para: Merlin Morgenstern CC: php-general@lists.php.net Enviado: mié,14 octubre, 2009 12:44 Asunto: Re: [PHP] regex for multiple line breakes 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
Re: [PHP] regex for multiple line breakes
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
Re: [PHP] regex for multiple line breakes
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: [PHP] regex for multiple line breakes
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