Page 1 of 1
New Lines
Posted: Fri Mar 09, 2012 12:39 pm
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.
Re: New Lines
Posted: Fri Mar 09, 2012 1:04 pm
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.
Re: New Lines
Posted: Fri Mar 09, 2012 1:19 pm
by spacebiscuit
Thanks - your code looks neater than mine.
It's still not catching the new line "\n" when I test it though!
Thanks...
Re: New Lines
Posted: Fri Mar 09, 2012 1:31 pm
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";
Re: New Lines
Posted: Fri Mar 09, 2012 1:37 pm
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))
Re: New Lines
Posted: Fri Mar 09, 2012 4:40 pm
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?
Re: New Lines
Posted: Sat Mar 10, 2012 4:03 am
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.
Re: New Lines
Posted: Sat Mar 10, 2012 12:54 pm
by requinix
Yes it should. Did you nl2br() anything?
Re: New Lines
Posted: Sat Mar 10, 2012 1:00 pm
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.
Re: New Lines
Posted: Mon Mar 12, 2012 8:54 am
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?
Re: New Lines
Posted: Mon Mar 12, 2012 3:31 pm
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:
Or, if you just want to match your hard-coded new lines:
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.
Re: New Lines
Posted: Wed Mar 14, 2012 8:53 am
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.
Re: New Lines
Posted: Wed Mar 14, 2012 9:07 am
by spacebiscuit
By the way:
$regex=',[\n\r\x0a\x0d]|\\\n,';
Works perfectly - thanks!
Re: New Lines
Posted: Wed Mar 14, 2012 2:37 pm
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.)