|
 |
|
โค้ดตัดตัวอักษรเหลือแต่ตัวเลขในไฟล์.txt และโค้ดอ่านไฟล์ .txt และบันทึกลงฐานข้อมูล เอา2 โค้ดนี้มาร่วมกันยังไงครับ |
|
 |
|
|
 |
 |
|
ข้อความทีใช้
Code (PHP)
$a="25/01/2011 9:35:00 Strat download.....
ID: 11111Date:2011-01-25 09:31
ID: 2Date:2011-01-25 09:31
ID: 4Date:2011-01-25 09:32
Success..."; //ข้อความที่จะเอามาทำ
เป็นรูปแบบ ID: 11111Date:2011-01-25 09:31 เหมือนกันหรือป่าวครับ ถ้ารูปแบบเดียวกันน่าจะใช้การหาตำแหน่งแล้วตัดข้อความออกมาได้
ตัวอย่าง
Code (PHP)
<?php
$strFileName = "text.txt";//ชื่อไฟล์. txt
$objFopen = fopen($strFileName, 'r');
$rows_num = 0; //สำหรับนับแถว
if($objFopen) {
while (!feof($objFopen)) {
$file = fgets($objFopen, 4096);
if($rows_num > 0){ //แถวแรกไม่เอาให้เริ่มจากแถว 2
$date_position = strpos($file, "Date:"); //หาตำแหน่งที่เริ่มของ Date
$file_string_1 = substr($file, 4, $date_position - 4); //ลบ 4 เพราะ Date มี 4 ตำแหน่ง
$file_string_2 = substr($file, $date_position + 5); // บวก 5 เพราะให้เริ่มจากตำแหน่งของ : เป็นต้นไป
$strSQL = "INSERT INTO test ";
$strSQL .=" (id_finger, datetime) ";
$strSQL .="VALUES";
$strSQL .="('".$file_string_1."', '".$file_string_2."') ";
$objQuery = mysql_query($strSQL);
}
$rows_num++;
}
fclose($objFopen);
}
?>
ลองประยุกต์ใช้ดูครับ เขียนแบบบ้านๆ
|
 |
 |
 |
 |
Date :
2011-02-01 12:17:03 |
By :
zankumuro |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ท่าน
|
ประวัติการแก้ไข 2011-02-01 13:38:27
 |
 |
 |
 |
Date :
2011-02-01 12:41:59 |
By :
hwungnee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมต้องการตัดบรรทัดสุดท้ายในไฟล์ .txt ออกด้วยทำไงครับ
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2011-02-01 13:39:26 |
By :
hwungnee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี้ครับไปแก้มาได้แล้ว มึนไปหน่อย อิอิ
Code (PHP)
<?php
include "connect.php";
$strFileName = "250120110935.txt";//ชื่อไฟล์. txt
$objFopen = fopen($strFileName, "r");
$rows_num = 0; //สำหรับนับแถว
if($objFopen) {
while (!feof($objFopen)) {
$file = fgets($objFopen, 4096);
if($rows_num > 0){ //แถวแรกไม่เอาให้เริ่มจากแถว 2
$date_position = strpos($file, "Date:"); //หาตำแหน่งที่เริ่มของ Date
$file_string_1 = substr($file, 4, $date_position - 4); //ลบ 4 เพราะ Date มี 4 ตำแหน่ง
$file_string_2 = substr($file, $date_position + 5); // บวก 5 เพราะให้เริ่มจากตำแหน่งของ:เป็นต้นไป
$file_string_3 = substr($file, $date_position +10);
$file_string_4 = substr($file, $date_position +13);
$file_string_5 = substr($file, $date_position +16);
$file_string_6 = substr($file, $date_position +19);
//$file_string_7 = substr($file, $date_position +10);
$strSQL = "INSERT INTO test ";
$strSQL .=" (id_finger,day,Month,year,hour,Minutes ) ";
$strSQL .="VALUES";
$strSQL .="('".$file_string_1."', '".$file_string_4."', '".$file_string_3."', '".$file_string_2."', '".$file_string_5."', '".$file_string_6."') ";
$objQuery = mysql_query($strSQL);
}
$rows_num++;
}
fclose($objFopen);
}
echo "<script>alert('จัดเก็บข้อมูลเรียบร้อยแล้ว ');
window.location.href ='header.php';</script>";
?>
ผลลัพ ในฐานข้อมูล

จะเห็นได้ว่ายังติด บรรทัดสุดท้ายยังแก้ไม่ได้
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2011-02-01 15:17:54 |
By :
hwungnee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|