2014-01-24 20 views
-1

對不起,我搜索了高和低的答案,我很難過。基本上我想要做的是用圖像替換某個單詞。用PHP輸出CSV文件到HTML talbe - 如何用圖像替換單詞?

我有一個CSV文件,我使用PHP代碼將數據輸出到HTML表格中。有沒有辦法讓php找到「微笑」這個詞的所有實例?並用這樣的東西代替它

<img src="<?php bloginfo('template_directory'); ?>/images/smile.jpg" /> 

這樣,HTML將輸出圖形而不是單詞「smile!」?

還有一個想法,我不知道我是否可以將PHP切換到類似template_directory的東西......也許將它切換到「http://website.com/images/smile.jpg」更容易?

確定在這裏使用匿名建議我寫這是我的完整代碼,但它仍然無法正常工作。有什麼建議麼?

$path_to_file = "http://....conquer.csv"; 
$smile = file_get_contents($path_to_file); 
$smile = str_replace("smile!","<img src=\"http://.........../images/smile.png\" />",$smile); 
file_put_contents($path_to_file,$smile); 

echo "<table id=\"conquer-table\">\n"; 

$row = 0; 
$handle = fopen("http://.........oad/conquer.csv", "r"); 

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
    if ($row == 0) { 
    // this is the first line of the csv file 
    // it usually contains titles of columns 
    $num = count($data); 
    echo "<thead>\n<tr>"; 
    $row++; 
    for ($c=0; $c < $num; $c++) { 
     echo "<th>" . $data[$c] . "</th>"; 
    } 
    echo "</tr>\n</thead>\n\n<tbody>"; 
} else { 
    // this handles the rest of the lines of the csv file 
    $num = count($data); 
    echo "<tr>"; 
    $row++; 
    for ($c=0; $c < $num; $c++) { 
     echo "<td>" . $data[$c] . "</td>"; 
    } 
    echo "</tr>\n"; 
    } 
    } 

fclose($handle); 

echo "</tbody>\n</table>"; 

?> 
+0

對不起,我忘了添加塊代碼,感謝非常快的負面評價!大聲笑 – randyttp

回答

1

您可以輕鬆地str_replace做到這一點:

$string = /*FULL TEXT HERE*/; 
$string = str_replace('smile!','<img src="<?php bloginfo(\'template_directory\'); ?>/images/smile.jpg" />',$string); 

對於它與不同的文本替換它,只需更換<img src="<?php bloginfo(\'template_directory\'); ?>/images/smile.jpg" />別的東西,和smile!代表什麼代碼所期待的。

+0

@randyttp這裏是一個例子:http://phpfiddle.org/main/code/cin-zzt按運行查看結果。如果它仍然無法正常工作,您需要更具體地瞭解哪些功能無法正常工作。 – Anonymous

+0

這並不適合我,但我用我的完整代碼更新了我的原始問題,所以也許你可以更好地看到我想要實現的目標...... CSV文件將會包含我需要替換的文字。 – randyttp

+0

在這種情況下,您可以將'$ string'設置爲'file_get_contents('http://.........oad/conquer.csv')',代碼將替換'smile! '它在那個文件中找到新文本並將其設置爲新的'$ string'變量。 – Anonymous