Bug #61054 [Opn->Nab]: preg_match () matching end of line in text file of windows style make a mistake
| From: | rasmus@php.net | Date: | Wed, 15 Feb 2012 05:19:46 +0000 |
| Subject: | Bug #61054 [Opn->Nab]: preg_match () matching end of line in text file of windows style make a mistake | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-167530@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=61054&edit=1
ID: 61054
Updated by: rasmus@php.net
Reported by: gzpan123 at gmail dot com
Summary: preg_match () matching end of line in text file of
windows style make a mistake
-Status: Open
+Status: Not a bug
Type: Bug
Package: PCRE related
Operating System: Windows,linux all
PHP Version: 5.3.10
Block user comment: N
Private report: N
New Comment:
Ok. here is my guess at what is confusing you.
By default PCRE uses \n to indicate the end of a line. Your files have \r\n at
the end, so I think the only confusion you have is that your second match will
have \r included in it and when you then print this on the console it looks
essentially invisible.
In your latest example there, try changing your output line to this:
echo $matches[1].trim($matches[2]);
Does it make more sense now?
Previous Comments:
------------------------------------------------------------------------
[2012-02-15 04:48:43] gzpan123 at gmail dot com
$matches[2] follow character <CR>
------------------------------------------------------------------------
[2012-02-15 04:44:28] gzpan123 at gmail dot com
Test script:
---------------
<?php
$file = fopen("test.txt","r");
while(!feof($file)){
$line = fgets($file);
preg_match('/^(.+) (.+)$/',$line,$matches);
echo $line;
if(array_key_exists(1, $matches) && array_key_exists(2, $matches))
echo $matches[1].$matches[2];
}
fclose($file);
?>
test.txt must be created in windows
abc def
ghi jklm
nop
Expected result:
----------
abc def
abcdef
ghi jklm
ghijklm
nop
Actual result:
----------
abc def
ghi jklm
nopjklm
------------------------------------------------------------------------
[2012-02-15 01:06:29] aharvey@php.net
I don't understand the issue either. The code behaves the same way for me
regardless of the line ending in test.txt.
Can you provide a simpler example, please?
------------------------------------------------------------------------
[2012-02-12 12:42:59] nikic@php.net
I don't really understand the issue you have from your example, but in general LF is the
compile-default linebreak for PCRE. If you want to use a different linebreak character you can
either compile PCRE with the appropriate option or specify a control option like (*CRLF) before your
regular expression.
------------------------------------------------------------------------
[2012-02-11 09:47:27] gzpan123 at gmail dot com
windows,linux all meet this bug
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=61054
--
Edit this bug report at https://bugs.php.net/bug.php?id=61054&edit=1