您可以尝试这样的方法来提取所有发票编号:
<?php
$input = "<body>
<p>Invoice ID: 0201</p>
<p>MID : Q987</p>
<p>Desciption: Solid Concrete Blocks</p>
<p>Qty: 7478 Blocks </p>
<p> </p>
<p>Invoice ID: 0324</p>
<p>MID : Q443</p>
<p>Desciption: Window Slides with Chrome </p>
<p>Qty: 33 Units </p>
</body>";
$invoice_ids = array();
if(preg_match_all('{<p>Invoice ID:\s*(\d+)</p>}',$input,$matches)) {
$invoice_ids = $matches[1];
}
var_dump($invoice_ids);
?>
输出:
array(2) {
[0]=>
string(4) "0201"
[1]=>
string(4) "0324"
}
提取所有发票编号后,您可以使用以下方法将它们插入数据库表中:
<?php
$con = mysql_connect("localhost","USRNAME","PASSWD");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DB_NAME", $con);
foreach($invoice_ids as $id) {
$query = "INSERT INTO YOUR_TABLE_NAME(invoice_num) VALUES $id";
if(!mysql_query($query,$con)) {
die('Error: ' . mysql_error());
}
}
mysql_close($con)
?>