目标:在“$ column4”中获取包含多个日期的数据条目,并为每个日期创建一个包含$ column2,$ column3,$ column4和日期后跟一条评论的新条目。
对于这个例子,我们说:
$column1 is an auto-incrementing primary ID
$column2 = '20141122001';
$column3 = 'something';
$column4 = 'else';
$string = '12/29/2014 2:44PM - working with the lender to remove the mortgage late so we are able to refinance the client. - Person 1 12/04/2014 2:27PM - file suspended until rapid rescore comes back removing late payment from credit. - Person 2';
以下示例适用于两个日期,但1-20个日期怎么样?提前谢谢。
<?php
$host = 'localhost';
$db_user = 'intergl8_james';
$db_pass = 'Interglobalsecure2014';
$db_name = 'intergl8_test';
try {
$pdo = new PDO('mysql:host='.$host.';dbname='.$db_name.'', $db_user, $db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('SELECT * FROM test');
$stmt->execute();
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
$string = $row[4];
// $column1 = $row[0]; just the ID, don't need to copy
$column2 = $row[1];
$column3 = $row[2];
$column4 = $row[3];
?>
<?php
echo $string . "<br>";
if(preg_match('/(.*)([0-9]{2}\/[0-9]{2}\/[0-9]{2,4})(.*)/', $string, $matches))
{
$date = $matches[2];
}
echo $date;
//////////////////////
if (isset($date)) {
$newstring = str_replace($date,"",$string);
if(preg_match('/(.*)([0-9]{2}\/[0-9]{2}\/[0-9]{2,4})(.*)/', $newstring, $matches))
{
$date2 = $matches[2];
}
}
echo "<br>";
echo $date2;
////////////////
echo "<br>";
$parts = parse_url($string);
$path_parts= explode($date2, $parts[path]);
$user = $path_parts[1];
$user2 = $path_parts[0];
//$user3 = $path_part[2];
echo $date2 . " " . $user . "<br>";
echo $user2 . "<br>";
$newdate = $date2 . " " . $user;
//echo $user3;
?>
<?php
$db_user = 'intergl8_james';
$db_pass = 'Interglobalsecure2014';
try {
$pdo = new PDO('mysql:host=localhost;dbname=intergl8_test', $db_user, $db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('INSERT INTO test2 VALUES(:id,:number,:blah,:cool,:notes)');
$stmt->execute(array(
':id' => '', ':number' => $column2, ':blah' => $column3, ':cool' => $column4, 'notes' => $newdate
));
$stmt->execute(array(
':id' => '', ':number' => $column2, ':blah' => $column3, ':cool' => $column4, 'notes' => $user2
));
# Affected Rows?
echo $stmt->rowCount() ." row(s) inserted."; // 1
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
?>
<?php
}
} else {
echo "No rows returned.";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
更新:
<?php
$host = 'localhost';
$db_user = '';
$db_pass = '';
$db_name = '';
try {
$pdo = new PDO('mysql:host='.$host.';dbname='.$db_name.'', $db_user, $db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('SELECT * FROM test');
$stmt->execute();
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
$string = $row[4];
// $column1 = $row[0]; just the ID, don't need to copy
$column2 = $row[1];
$column3 = $row[2];
$column4 = $row[3];
?>
<?php
preg_match_all('/(.*)([0-9]{2}\/[0-9]{2}\/[0-9]{2,4})(.*)/', $string, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
echo "matched: " . $val[0] . "<br>";
/* echo "part 1: " . $val[1] . "<br>";
echo "part 2: " . $val[2] . "<br>";
echo "part 3: " . $val[3] . "<br>";
echo "part 4: " . $val[4] . "<br><br>"; */
// start crazy shit
$db_user = '';
$db_pass = '';
$db_name = '';
try {
$pdo = new PDO('mysql:host=localhost;dbname='..'', $db_user, $db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('INSERT INTO test2 VALUES(:id,:number,:blah,:cool,:notes)');
$stmt->execute(array(
':id' => '', ':number' => $column2, ':blah' => $column3, ':cool' => $column4, 'notes' => $val[0]
));
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
// end crazy shit
}
?>
<?php
}
} else {
echo "No rows returned.";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>