2014-09-22 2 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.      = 

코드를

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); 
관련 문제