Edit report at https://bugs.php.net/bug.php?id=61054&edit=1
ID: 61054
User updated by: gzpan123 at gmail dot com
Reported by: gzpan123 at gmail dot com
Summary: preg_match () matching end of line in text file of
windows style make a mistake
-Status: Feedback
+Status: Open
Type: Bug
Package: PCRE related
Operating System: Windows,linux all
PHP Version: 5.3.10
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2012-02-15 01:06:29] [email protected]
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] [email protected]
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
------------------------------------------------------------------------
[2012-02-11 09:42:45] gzpan123 at gmail dot com
Description:
------------
function:preg_ match()
when processing text file of windows style like blow example,it get wrong
the $matches[2] behind follow a character <CR>,
----
Create a text file(test.txt) in windowsï¼Content:
hello guo
hi jason
test
Test script:
---------------
<?php
$file = fopen("test.txt","r");
while(!feof($file)){
$line = fgets($file);
preg_match('/^(.+) (.+)$/',$line,$matches);
print_r($matches);
echo $line;
echo $matches[1].$matches[2];
}
fclose($file);
?>
Expected result:
----------------
Array
(
[0] => hello guo
[1] => hello
[2] => guo
)
hello guo
helloguo
Array
(
[0] => hi jason
[1] => hi
[2] => jason
)
hi jason
hijason
Array
(
)
test
Actual result:
--------------
hello guo
Array
(
[0] => hello guo
[1] => hello
[2] => guo
)
hi jason
Array
(
[0] => hi jason
[1] => hi
[2] => jason
)
testArray
(
)
test
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61054&edit=1