Php 在文本文件中匹配数字?

Php 在文本文件中匹配数字?,php,preg-match,Php,Preg Match,我想匹配文本文件中的数字 并将其替换为添加到匹配号码中的新号码 似乎无法让它工作 <!---- 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&

我想匹配文本文件中的数字 并将其替换为添加到匹配号码中的新号码

似乎无法让它工作

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



?>

正如anubhava在评论中所说,您必须用类似于
/
的分隔符包围您的正则表达式,并添加paren以捕获名称组:

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

您的
$pattern
没有分隔符。它应该是:
$pattern=“/?P\d+/”