0

I want to match the numbers in the textfile and replace it with the new one added to the matched number

cant seem to get it working

<!---- 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); 



?>
4

1 に答える 1

0

anubhava がコメントで述べたように、正規表現を次のような区切り記号で囲み、/括弧を追加して名前 group をキャプチャする必要があります。

$pattern = "/(?P<digit>\d+)/";
preg_match($pattern, $Data, $matches);
于 2013-04-08T08:17:42.883 に答える