2014-09-22 43 views
-1

我有以下的文本文件:獲取的文本文件中的特定句子

==================================================================================== 
INDEXNUMARTICLE: '1997' 
FILE: '###\www.kkk.com\kompas-pront\0004\25\economic\index.htm' NUMSENT: '22' DOMAIN: 'economic' 
==================================================================================== 

2. Social change is a general term which refers to: 
4. change in social structure: the nature, the social institutions. 
6. When behaviour pattern changes in large numbers, and is visible and sustained, it results in a social change. 

我想只得到了一句沒有編號,並將其保存在數據庫:

========================================================================= 
= id = topic =      content       = 
========================================================================= 
= 1 = economic = Social change is a general term which refers to:  = 
       = change in social structure: the nature,    = 
       = the social institutions. When behaviour pattern  = 
       = changes in large numbers, and is visible and sustained, 
       = it results in a social change.      = 

CODE

function isNumber($string) { 
    return preg_match('/^\\s*[0-9]/', $string) > 0; 
} 

$txt = "C:/Users/User/Downloads/economic.txt"; 
$lines = file($txt); 

foreach($lines as $line_num => $line) { 
$checkFirstChar = isNumber($line); 
if ($checkFirstChar !== false) { 
    $line_parts = explode(' ', $line); 
    $line_number = array_shift($line_parts); 

    foreach ($line_parts as $part) { 
     if (empty($part)) continue; 
     $parts = array(); 
     $string = implode(' ', $parts); 
     $query = mysql_query("INSERT INTO tb_file VALUES ('','economic','$string')"); 
    } 
} 

}

我對數組有問題,在列內容中插入的數據是由不同行中的單詞組成的。請幫幫我。謝謝:)

回答

0

我認爲你的想法是複雜的 - 試試這個短的一個

$txt = "C:/Users/User/Downloads/economic.txt"; 
$lines = file($txt); 
foreach($lines as $line_num => $line) { 
    $checkFirstChar = isNumber($line); 
    if ($checkFirstChar !== false) { 
     //entire text line without number 
     $string = substr($line,strpos($line,"")+1); 
     $query = mysql_query("INSERT INTO tb_file VALUES ('','economic','$string')"); 
    } 
} 
0

試試這一個,用正則表達式。

$regex = "/[0-9]\. /"; 

$txt = "C:/Users/User/Downloads/economic.txt"; 
$str = file_get_contents($txt); 
$index = -1; 

//Find the first ocurrence of a number followed by '.' and a whitespace 
if(preg_match($regex, $str, $matches, PREG_OFFSET_CAPTURE)) { 
    $index = $matches[0][1]; 
} 

//Remove all the text before that first occurrence 
$str = substr($str, $index); 

//Replace all the occurrences of number followed by '. ' with ' ' 
$text = preg_replace($regex, " ", $str);