New Lines

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

New Lines

Post by spacebiscuit »

Hi,

I'm trying to check for new lines a string using:

Code: Select all

if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $string)) {
     echo "do something";
                                                                            }
I have tried several online tools and it correctly finds occurences of "\n". However when I run this script it does not flag "\n".

Thanks in advance.
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: New Lines

Post by ragax »

Hi spacebiscuit,

Try

Code: Select all

if(preg_match("~[\n\r]~", $string))
Several tweaks to your code:
- does not need to be case insensitive, so removed the i
- with \\n, you are escaping the backslash, so the engine is looking for the literal characters "\n", instead of a new line. \n is all you need.
- to look for a single character, a character class [inside brackets] is more efficient than al|ter|nations.

Please lee me know how this works for you.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: New Lines

Post by spacebiscuit »

Thanks - your code looks neater than mine.

It's still not catching the new line "\n" when I test it though!

Thanks...
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: New Lines

Post by ragax »

Strange, it works for me.

I'd suggest having a look at the delimiters used for new lines in your text. For instance by running something like this:

Code: Select all

$string = 'New
Line';
for($i=0;$i<strlen($string);$i++) echo "{{$string[$i]}}" . ord($string[$i]) . " <br />\n";
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: New Lines

Post by ragax »

Also, remembering that you were expecting the Ascii 0a or 0d, you could incorporate them like so:

Code: Select all

if(preg_match("~[\n\r\x0a\x0d]~", $string))
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: New Lines

Post by requinix »

playful's regex will catch newlines. If it doesn't catch what you have then you don't have newlines.

Could they be <br>s?
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: New Lines

Post by spacebiscuit »

the input I am trying to capture is coming from an html text input field.

I am testing with a string such as:

'test\ntest'

Should the regex I am using catch this?

Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: New Lines

Post by requinix »

Yes it should. Did you nl2br() anything?
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: New Lines

Post by ragax »

And did you try the for loop I gave you?
Please post the results (using your string as input), that will tell us exactly what we're looking at. (Like requinix, curious about the br story, or weird encoding, who knows.) Thx.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: New Lines

Post by spacebiscuit »

Apologies for the late reply, the weekend got in the way!

ok I have tried the for loop suggestion on the following:

Code: Select all

$string = 'new\nline';

{n}110
{e}101
{w}119
{\}92
{n}110
{l}108
{i}105
{n}110
{e}101
So looks like it is not picking up the new line that I have inserted right?
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: New Lines

Post by ragax »

Hi Space Biscuit!

Okay, so we are not trying to match an actual new line, as in :
$string="The new line is between this arrow=>
<=and the one to the left";

Instead, we are trying to match a new line that you are trying to hard-code.
Keeping what we had before and adding to it, you can use:

Code: Select all

$regex=',[\n\r\x0a\x0d]|\\\n,';
Or, if you just want to match your hard-coded new lines:

Code: Select all

$regex=',\\\n,';
And you can run this to check that it works:

Code: Select all

<?php
$regex=',\\\n,';
$string='new\nline';
echo preg_match($regex,$string);
?>
(Returns 1.)

Though to match a plain string (rather one member in a set of strings), strpos is preferred to regex.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: New Lines

Post by spacebiscuit »

Thanks for the reply.

i'm trying to check for malicious code that may have been added to an html input string. So I am looking for the hard code, but sohlud I also be looking for the actual new line also?

Thanks.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: New Lines

Post by spacebiscuit »

By the way:

$regex=',[\n\r\x0a\x0d]|\\\n,';

Works perfectly - thanks!
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: New Lines

Post by ragax »

Hi Spacebiscuit,

Glad we got this simple regex working for you at last!
should I also be looking for the actual new line also?
No idea... that really depends on what you're expecting malicious code to look like---it's a security question. (btw I just noticed there's a security board.)
Post Reply