2015-04-22 75 views
0

我無法編寫用於檢查新聞關鍵字的代碼如果存在5個關鍵字中的2個匹配項。腳本將在新數據庫中插入此帖子。檢查帖子中的關鍵詞並找到匹配項

這是我的代碼,但它只檢查一個關鍵字,如果它存在它將數據庫中插入此新聞。

$sq = 'SELECT Keyword FROM keyword'; 
$receivekey = mysqli_query($conn,$sq); 
if(! $receivekey) 
{ 
    die('Could not load data: ' . mysql_error()); 
} 

while($row = mysqli_fetch_array($receivekey)){ 
    $itemkey[] = $row['Keyword']; 
} 
//echo '<br>'.$itemkey[1]; 
foreach($itemkey as $newkey => $value){ 
    echo '<br>'.$value; 
    //print_r($value); 
} 

mysqli_close($conn); 

//select the correct xml for social media 

//read xml and check for preference 
//$content = simeplexml_load_file('facebook.xml'); 
$xml = simplexml_load_file('facebook.xml'); 
foreach($xml->Tue14Apr2015 as $entry); 
foreach($entry->post as $new){ 
    $new = strtolower($new); 
     if (is_array($itemkey)) 
    { 
     foreach($itemkey as $e => $ne){  
      $pos = strpos($new, $ne); 
      if ($pos == true){ 
       echo '<br>'.$new; 

回答

0

嘗試使用substr_count,像這樣:

$occurrence = 0; 

foreach($itemkey as $key){ 
    $occurrence += substr_count($news, $key); 
} 

if($occurrence >= 2) { 
    // Insert news in database here 
} 

此遞增OCCURENCES的量爲找到的每個關鍵字。