2011-10-06 77 views
0

我有這個的file_get_contents返回& g t ;代替<

$this->CompileString_ = file_get_contents("test.html"); 
echo $this->CompileString_; 

的test.html

<div>test</div> 
<div>test</div> 

的問題是,返回的file_get_contents的&gt;而不是讓<

爲什麼?

我使用wampserver 2 PHP 5.3.8

+0

也許你可以給我們一張您試圖檢索的文本。 –

+0

我在示例中添加一個test.html文件以獲得更多的清晰度 –

+0

我試過了你的代碼,它似乎沒問題。對我來說,函數返回「<」字符。你能更清楚地知道你真正看到了什麼嗎?我的意思是......當你迴應這個字符串時,你看到了這個? < div < test </div < –

回答

1

你好嘗試將文件重命名爲test.txt也許PHP的自動編碼爲.html文件。

使用htmlentitydecode();

<?php 
    $orig = "I'll \"walk\" the <b>dog</b> now"; 

    $a = htmlentities($orig); 

    $b = html_entity_decode($a); 

    echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now 

    echo $b; // I'll "walk" the <b>dog</b> now 


    // For users prior to PHP 4.3.0 you may do this: 
    function unhtmlentities($string) 
    { 
     // replace numeric entities 
     $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string); 
     $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string); 
     // replace literal entities 
     $trans_tbl = get_html_translation_table(HTML_ENTITIES); 
     $trans_tbl = array_flip($trans_tbl); 
     return strtr($string, $trans_tbl); 
    } 

    $c = unhtmlentities($a); 

    echo $c; // I'll "walk" the <b>dog</b> now 

    ?> 
+0

我想你回答錯了。 OP想要解碼字符而不是編碼它們。 –

+0

@AurelioDeRosa對不起,修正。 – Mob

+0

別擔心。這有可能發生。 –