PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jul 23, 2019 1:41 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: New Lines
PostPosted: Fri Mar 09, 2012 1:39 pm 
Offline
Forum Contributor

Joined: Mon Mar 07, 2005 4:20 pm
Posts: 390
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.


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Fri Mar 09, 2012 2:04 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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.


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Fri Mar 09, 2012 2:19 pm 
Offline
Forum Contributor

Joined: Mon Mar 07, 2005 4:20 pm
Posts: 390
Thanks - your code looks neater than mine.

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

Thanks...


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Fri Mar 09, 2012 2:31 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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";
 


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Fri Mar 09, 2012 2:37 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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))


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Fri Mar 09, 2012 5:40 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
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?


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Sat Mar 10, 2012 5:03 am 
Offline
Forum Contributor

Joined: Mon Mar 07, 2005 4:20 pm
Posts: 390
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.


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Sat Mar 10, 2012 1:54 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Yes it should. Did you nl2br() anything?


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Sat Mar 10, 2012 2:00 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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.


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Mon Mar 12, 2012 8:54 am 
Offline
Forum Contributor

Joined: Mon Mar 07, 2005 4:20 pm
Posts: 390
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?


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Mon Mar 12, 2012 3:31 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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.


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Wed Mar 14, 2012 8:53 am 
Offline
Forum Contributor

Joined: Mon Mar 07, 2005 4:20 pm
Posts: 390
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.


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Wed Mar 14, 2012 9:07 am 
Offline
Forum Contributor

Joined: Mon Mar 07, 2005 4:20 pm
Posts: 390
By the way:

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

Works perfectly - thanks!


Top
 Profile  
 
 Post subject: Re: New Lines
PostPosted: Wed Mar 14, 2012 2:37 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group