Php 什么';代码的意思是什么?
txt文件如下所示:Php 什么';代码的意思是什么?,php,Php,txt文件如下所示: $file = fopen("test.txt","r"); while($line = fgets($file)) { $line = trim($line); list($model,$price) = preg_split('/\s+/',$line); if(empty($price)) { $price = 0; } $sql = "UPDATE products SET products_price=$price
$file = fopen("test.txt","r");
while($line = fgets($file)) {
$line = trim($line);
list($model,$price) = preg_split('/\s+/',$line);
if(empty($price)) {
$price = 0;
}
$sql = "UPDATE products
SET products_price=$price
WHERE products_model='$model'";
// run the sql query.
}
fclose($file);
1、list($model,$price)=preg_split('/\s+/',$line)的含义是什么代码>
我知道preg_split
就像explode
,但我不知道上面这行的参数含义是什么
2、如何跳过第一条记录。它将获取preg_split
的结果,并将其分配给变量$model
和$price
。您正在查看解析算法。对不起,这还不够。我很难理解这个问题写下来的样子
另外,如果我读对了,就没有必要跳过第1行,除非数据库中有一个模型定义为“model”的项
但是如果你出于某种原因想要,你可以添加一个计数器
model price
LB2117 19.49
LB2381 25.99
这是一种允许您同时分配多个变量的语言构造。您可以将其视为数组解包(preg\u split
返回一个数组)。因此,当您这样做时:
$i = 0;
while($line = fgets($file)) {
if($i > 0)
{
$line = trim($line);
list($model,$price) = preg_split('/\s+/',$line);
if(empty($price)) {
$price = 0;
}
$sql = "UPDATE products
SET products_price=$price
WHERE products_model='$model'";
// run the sql query.
}
$i++;
}
列表中的元素少于数组是可以的,数组中多余的元素将被忽略,但数组中元素不足将导致未定义的索引错误。例如:
a
b
我不知道你是不是想跳过第一张唱片但是
list($a) = explode(".","a.b"); // ok
list($a,$b,$c) = explode(".","a.b") // error
但是当我运行代码时,它显示了一个错误,请注意:第11行的D:\www\update.php中的未定义偏移量:1。第11行是list($model,$price)=preg_split('/\s+/',$line);但是当我运行代码时,它显示了一个错误,请注意:第11行的D:\www\update.php中的未定义偏移量:1。第11行是list($model,$price)=preg_split('/\s+/',$line);
list($a) = explode(".","a.b"); // ok
list($a,$b,$c) = explode(".","a.b") // error
$file = fopen("test.txt","r"); // open file for reading
$first = true;
while($line = fgets($file)) { // get the content file
if ($first === true) { $first = false;}//skip the first record
else{
$line = trim($line); // remove the whitespace before and after the test
// not in the middle
list($model,$price) = preg_split('/\s+/',$line); // create two variable model and price, the values are from the preg_split \s means whitespace, tab and linebreak
if(empty($price)) { // if $price is empty price =0
$price = 0;
}
$sql = "UPDATE products // sql update
SET products_price=$price
WHERE products_model='$model'";
// run the sql query.
}
}
fclose($file); //close the file