2013-04-07 94 views
0

我想在文本文件 匹配的數字,並用新的替換它添加到匹配的數量匹配文本文件中的數字?

不能似乎得到它的工作

<!---- update textfile on the server after a new order has taken place---> 
<?php 


$filename = "order.txt"; 
$handle = fopen($filename, "r"); 

$Data = fread($handle, 512); 
echo $Data; 
$pattern = "?P<digit>\d+"; 
preg_match($pattern,$Data,$matches); 
print_r($matches); 


fclose($handle); 


$Handle = fopen($File, 'w'); 
fwrite($Handle, "The total Number of Apples: ".$Apples.PHP_EOL); 
fwrite($Handle, "The total Number of Bananas: ".$Bananas.PHP_EOL); 
fwrite($Handle, "The total Number of Oranges: ".$Oranges.PHP_EOL); 

fclose($Handle); 



?> 
+1

你'$ pattern'沒有分隔符。它應該是:'$ pattern =「/?P \ d + /」;' – anubhava 2013-04-07 07:57:02

回答

0

由於anubhava在評論中說,你要環繞你的正則表達式與像/分隔符,並添加括號來捕捉姓名組:

$pattern = "/(?P<digit>\d+)/"; 
preg_match($pattern, $Data, $matches);