2012-03-02 81 views
0

我有輸出電影審查制度,當輸出我想,出現在它從2代表的關鍵字 - 消極和積極的是在​​一個不同顏色的任何想法要做到這一點?代碼如下PHP輸出文本高亮

<?php 

// Connect to database 
mysql_connect("sdd", "sdsd", "") or die(mysql_error()); 
mysql_select_db("sdsd") or die(mysql_error()); 

$id = mysql_real_escape_string($_POST['reviewid']); 

//$query = "select * from review where id = '$id'"; 
$query = mysql_fetch_assoc(mysql_query("SELECT filmreview FROM review WHERE id = '$id'")); 
$pos = mysql_query("SELECT word FROM positive"); 
$neg = mysql_query("SELECT word FROM negative"); 


//Variables 
$review_text = $query['filmreview']; 
$good = 0; 
$bad = 0; 


// Gets words in to a text array and converts to lower case 
$cnt_r = array_count_values(array_map('mb_strtolower',str_word_count($review_text, 1))); 

// Gets the positive words and check for the word in the text 
    while($check = mysql_fetch_assoc($pos)){ 
     $lower = mb_strtolower($check['word']); 
    if(isset($cnt_r[$lower])){ 
    $good+= $cnt_r[$lower]; 
    echo $check ['word']; 
    echo "<p>"; 
    } 
    } 

// Gets the negative words and check for the word in the text 
while($check = mysql_fetch_assoc($neg)){ 
    $lower = mb_strtolower($check['word']); 
if(isset($cnt_r[$lower])){ 
    $bad+= $cnt_r[$lower]; 
     echo $check ['word']; 
     echo "<p>"; 
} 
} 


// If there are more positive words than negative than the review is positive 
if ($good > $bad) 
{ 
    echo "<p>"; 
    echo "This is a positive review"; 
    echo "<p>"; 
} 

// If there are more negative words than positive than the review is negative 
else if ($good < $bad) 
{ 
    echo "<p>"; 
    echo "This is a negative review"; 
    echo "<p>"; 
} 

// If there are the same amount of positive and negative words than the review is average 
else if ($good == $bad) 
{ 
    echo "<p>"; 
    echo "This is an average review"; 
    echo "<p>"; 
} 

//Prints out the number of postive and negative words found 
echo "Good words: " . $good . " and Bad words: " . $bad; 
echo "<p>"; 
echo $query ['filmreview']; 
echo "<p>"; 
echo "This is <font color=\"blue\">blue</font>!"; 



echo "<form method='post' action='welcome.html'>"; 
echo "<input type='submit' name='searchagain' value='Search'>"; 
?> 
+0

謝謝你的答覆,但我不能得到他們中的任何一個工作不知道什麼即時做錯了 – user1045566 2012-03-02 22:03:42

回答

0

只是搜索和查找關鍵字的文本,然後只替換它們。 所以大致

if($postivekeyword) { 
     $newpostivekeyword = '<span class="positive">'.$postivekeyword.'</span>'; 
    } 
    if($negativekeyword) { 
     $newnegativekeyword = '<span class="negative">'.$negativekeyword.'</span>'; 
    } 

然後,只需

$new_review_text = str_replace($postivekeyword, $newpostivekeyword, $review_text); 
+0

我如何將顏色包含在內?因爲沒有輸出 – user1045566 2012-03-02 15:27:01

+0

只需將類添加到您的css .negative {color:red; } 。正面{color:black; } 當然在文件中,你需要的地方 回聲$ new_review_text; – 2012-03-02 16:31:34

+0

由於某種原因,這似乎不工作 – user1045566 2012-03-02 21:40:42

2

您可以使用CSS類。

在你的樣式或風格標籤你頁面中您可以創建CSS類:

p .negative { color: red; } 
p .positive { color: black; } 

現在只需將該類添加到您的段落,它將應用所需的樣式。

// If there are more positive words than negative than the review is positive 
if ($good > $bad) 
{ 
    echo '<p class="positive">'; 
    echo 'This is a positive review'; 
    echo '</p>'; 
} 

// If there are more negative words than positive than the review is negative 
else if ($good < $bad) 
{ 
    echo '<p class="negative">'; 
    echo 'This is a negative review'; 
    echo '</p>'; 
} 

等.....

+0

但我想在文字中的文字是彩色不是評論類型 – user1045566 2012-03-02 17:17:51

+0

如果你只是從一個段落中選擇單詞顏色你可以使用相同的原子,只需使用一個span類,你可以追加到每個單詞的開始和結尾,而不是整個段落的類。 – chapman84 2012-03-02 17:29:38

-1
  1. 嘗試讀取查詢的響應你送後馬上。這樣一來,您就不必多次查詢的結果會堵塞你的記憶
  2. $cnt_r = array_count_values(array_map('mb_strtolower',str_word_count($review_text, 1)));似乎是$cnt_r = str_word_count(mb_strtolower($review_text), 2);
  3. str_word_count的fraked後續版本並不多字節安全的。在Smarty中,我使用preg_match_all('#[\w\pL]+#u', $review_text, $matches);作爲str_word_count()的UTF-8版本的基礎。
  4. PDO仔細一看
  5. 給你的一些變量標識姓名
  6. 始終正確esacape你的輸出 - 用htmlspecialchars()是否有被使用!
  7. 這是2012年 - 我們永遠不會使用<font>標籤!改用CSS!

<?php 
$query = mysql_fetch_assoc(mysql_query("SELECT filmreview FROM review WHERE id = '$id'")); 
$review_text = $query['filmreview']; 

// Gets words in to a text array and converts to lower case 
$review_words = array_count_values(array_map('mb_strtolower',str_word_count($review_text, 1))); 

$positive_words = array(); 
$negative_words = array(); 
$positive_words_count = 0; 
$negative_words_count = 0; 

// Gets the positive words and check for the word in the text 
$res = mysql_query("SELECT word FROM positive"); 
while ($row = mysql_fetch_assoc($res)){ 
    // why isn't this already stored lower-case in database? 
    $_word = mb_strtolower($row['word']); 
    if (isset($review_words[$_word])){ 
     $positive_words_count += $review_words[$_word]; 
     $positive_words[] = $_word; 
    } 
} 

// Gets the negative words and check for the word in the text 
$res = mysql_query("SELECT word FROM negative"); 
while ($row = mysql_fetch_assoc($res)){ 
    // why isn't this already stored lower-case in database? 
    $_word = mb_strtolower($row['word']); 
    if (isset($review_words[$_word])){ 
     $negative_words_count += $review_words[$_word]; 
     $negative_words[] = $_word; 
    } 
} 

if ($positive_words_count > $negative_words_count) { 
    // If there are more positive words than negative than the review is positive 
    echo "<p>This is a positive review<p>"; 
} elseif ($positive_words_count < $negative_words_count) { 
    // If there are more negative words than positive than the review is negative 
    echo "<p>This is a negative review<p>"; 
} else { 
    // If there are the same amount of positive and negative words than the review is average 
    echo "<p>This is an average review<p>"; 
} 

// highlight positive/negative words 
$review_text = htmlspecialchars($review_text); 
$pattern = '#\b(' . join('|', $positive_words) . ')\b#i'; 
$review_text = preg_replace($pattern, "<span class=\"positive\"\\1</span>", $review_text); 
$pattern = '#\b(' . join('|', $negative_words) . ')\b#i'; 
$review_text = preg_replace($pattern, "<span class=\"negative\"\\1</span>", $review_text); 

//Prints out the number of postive and negative words found 
echo "Good words: " . $positive_words_count . " and Bad words: " . $negative_words_count; 
echo "<p>" . $review_text . "<p>"; 
echo "This is <font color=\"blue\">blue</font>!"; 

,並在某處你的CSS定義span.negativespan.positive應該是什麼樣子。

+0

如何定義我從來沒有使用它之前 – user1045566 2012-03-02 16:51:21

+0

古爾的「介紹的CSS的CSS 「你會發現像http://www.w3.org/MarkUp/Guide/Style – rodneyrehm 2012-03-02 16:57:58

+0

犯規一堆有用的東西似乎工作 – user1045566 2012-03-02 21:54:52