PHP Developers Network
http://forums.devnetwork.net/

New Lines
http://forums.devnetwork.net/viewtopic.php?f=38&t=134786
Page 1 of 1

Author:  spacebiscuit [ Fri Mar 09, 2012 1:39 pm ]
Post subject:  New Lines

Hi,

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

Syntax: [ Download ] [ Hide ]
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.

Author:  ragax [ Fri Mar 09, 2012 2:04 pm ]
Post subject:  Re: New Lines

Hi spacebiscuit,

Try
Syntax: [ Download ] [ Hide ]
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.

Author:  spacebiscuit [ Fri Mar 09, 2012 2:19 pm ]
Post subject:  Re: New Lines

Thanks - your code looks neater than mine.

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

Thanks...

Author:  ragax [ Fri Mar 09, 2012 2:31 pm ]
Post subject:  Re: New Lines

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:
Syntax: [ Download ] [ Hide ]
$string = 'New
Line'
;
for($i=0;$i<strlen($string);$i++) echo "{{$string[$i]}}" . ord($string[$i]) . " <br />\n";
 

Author:  ragax [ Fri Mar 09, 2012 2:37 pm ]
Post subject:  Re: New Lines

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

Syntax: [ Download ] [ Hide ]
if(preg_match("~[\n\r\x0a\x0d]~", $string))

Author:  requinix [ Fri Mar 09, 2012 5:40 pm ]
Post subject:  Re: New Lines

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?

Author:  spacebiscuit [ Sat Mar 10, 2012 5:03 am ]
Post subject:  Re: New Lines

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.

Author:  requinix [ Sat Mar 10, 2012 1:54 pm ]
Post subject:  Re: New Lines

Yes it should. Did you nl2br() anything?

Author:  ragax [ Sat Mar 10, 2012 2:00 pm ]
Post subject:  Re: New Lines

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.

Author:  spacebiscuit [ Mon Mar 12, 2012 8:54 am ]
Post subject:  Re: New Lines

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

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

Syntax: [ Download ] [ Hide ]
$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?

Author:  ragax [ Mon Mar 12, 2012 3:31 pm ]
Post subject:  Re: New Lines

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:
Syntax: [ Download ] [ Hide ]
$regex=',[\n\r\x0a\x0d]|\\\n,';

Or, if you just want to match your hard-coded new lines:
Syntax: [ Download ] [ Hide ]
$regex=',\\\n,';


And you can run this to check that it works:
Syntax: [ Download ] [ Hide ]
<?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.

Author:  spacebiscuit [ Wed Mar 14, 2012 8:53 am ]
Post subject:  Re: New Lines

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.

Author:  spacebiscuit [ Wed Mar 14, 2012 9:07 am ]
Post subject:  Re: New Lines

By the way:

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

Works perfectly - thanks!

Author:  ragax [ Wed Mar 14, 2012 2:37 pm ]
Post subject:  Re: New Lines


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/