|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-02-11 09:42 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 17:00:01 2025 UTC |
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