* 不知道 javascript中 怎么 引入模板文件, 所以用js字符串拼接 html
* LineProcessor.php
<?php
class LineProcessor {
/** @var \SplFileObject */
private $file;
/** @var int */
private $linum;
/** @var callable */
protected $handler;
public function __construct(string $path) {
$this->file = new \SplFileObject($path, 'r');
$this->linum = 0;
}
/**
* 试着读取$limit行, 对每一行执行$callback
* @param callable $callback
* @param int $limit default unlimited
* @return int 实际读取的行数
*/
public function forEach(callable $callback, int $limit = 0) {
// unlimited
if (0===$limit) {
while ($this->file->valid()) {
$line = $this->file->fgets();
call_user_func($callback, $line, $this->linum);
$this->linum++;
}
return 0;
}
for ($i = 0; $i < $limit && $this->file->valid(); $this->linum++) {
$line = $this->file->fgets();
if (!empty($line)) {
call_user_func($callback, $line, $i);
$i++;
}
}
return $i;
}
public function setHandler(callable $cb) {
if (!$cb) {
$cb = function($cur, $i) {
printf("%d %s\n", $i, $cur);
};
}
$this->handler = $cb;
}
public function run() {
$this->forEach($this->handler);
}
}
* index.php
<?php
function __autoload($className) {
include $className.'.php';
}
$p = new \LineProcessor('./ruleform.html');
$p->setHandler(function($cur) {
$cur = trim($cur);
$len = strlen($cur);
if ($len === 0) {
return 0;
}
$cur = "\"" . addslashes($cur) . "\"+";
echo $cur.PHP_EOL;
return $len;
});
$p->run();
Usage:
php index.php > ruleform.js
Example #1 $argv example
<?php
var_dump($argv);
?>
When executing the example with: php script.php arg1 arg2 arg3
The above example will output something similar to:
array(4) {
[0]=>
string(10) "script.php"
[1]=>
string(4) "arg1"
[2]=>
string(4) "arg2"
[3]=>
string(4) "arg3"
}
* 每一列带引号的csv文件转换为sql
来自 mysqlworkbench "copy row"
rows.txt
'832185', '28432384', 'P40178', 'kol0e1s2i838u34ll842t7rp82', '0', '0', NULL, NULL, '2018-04-30 12:56:10', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', ''
'832423', '28548075', 'P40178', 'pkp8uabb9a3c83rhll4h5ib354', '0', '0', NULL, NULL, '2018-05-01 06:42:57', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', ''
'836811', '28615006', 'P40178', 'cg8ulgoigl82u06or1vs3nj6f6', '0', '0', NULL, NULL, '2018-05-19 04:11:59', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', ''
'837374', '28586092', 'P40178', 'i1bkk61rl2fka16406j5sn2796', '0', '0', NULL, NULL, '2018-05-21 04:29:09', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', ''
'841732', '28630342', 'P40178', 'tb7uu79734m963jglu3t2lq017', '0', '0', NULL, NULL, '2018-06-03 10:27:12', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', ''
'842292', '21338268', 'P40178', 'oti4jvg0o66je07savieblef24', '0', '0', NULL, NULL, '2018-06-06 01:09:41', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', ''
'844150', '0', 'P40178', 'qggksjob4ve28t9rjrr4obs316', '0', '0', NULL, NULL, '2018-06-13 08:03:59', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', ''
'845685', '0', 'P40178', 'j096g1me115dftsg2h6g24okd1', '0', '0', NULL, '{\"isnewpolicyarea\":0,\"ischoice\":0,\"province\":\"\",\"activemethod\":\"\"}', '2018-06-19 02:01:51', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', ''
index.php
使用 LineProcessor.php
<?php
function __autoload($className) {
include $className.'.php';
}
$p = new \LineProcessor('./rows.txt');
$fs = "id, userid, courseguid, sessionid, isdelete, isgenerateorder, ordernum, remarks, inserttime, updatetime, generateordertime, source, uuid";
$sql = "insert into W_ShoppingCart(".$fs.") values";
$p->setHandler(function($cur) use ($sql) {
$cur = substr($cur, 0, strlen($cur)-2); // get rid of \r\n
if (!empty($cur)) {
echo $sql.'('.$cur.');'.PHP_EOL;
}
});
$p->run();
* run:
php index.php > insert.sql
insert into W_ShoppingCart(id, userid, courseguid, sessionid, isdelete, isgenerateorder, ordernum, remarks, inserttime, updatetime, generateordertime, source, uuid) values('832185', '28432384', 'P40178', 'kol0e1s2i838u34ll842t7rp82', '0', '0', NULL, NULL, '2018-04-30 12:56:10', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '');
insert into W_ShoppingCart(id, userid, courseguid, sessionid, isdelete, isgenerateorder, ordernum, remarks, inserttime, updatetime, generateordertime, source, uuid) values('832423', '28548075', 'P40178', 'pkp8uabb9a3c83rhll4h5ib354', '0', '0', NULL, NULL, '2018-05-01 06:42:57', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '');
insert into W_ShoppingCart(id, userid, courseguid, sessionid, isdelete, isgenerateorder, ordernum, remarks, inserttime, updatetime, generateordertime, source, uuid) values('836811', '28615006', 'P40178', 'cg8ulgoigl82u06or1vs3nj6f6', '0', '0', NULL, NULL, '2018-05-19 04:11:59', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '1', '');
PHP 7.2之后不能用__autoload 参照php autoload 自动加载