【问题标题】:php string convert to htmlphp字符串转换为html
【发布时间】:2010-11-24 13:23:27
【问题描述】:

我有一个字符串<div id="myid">...</div>

我将如何将其更改为 <div id="myid">...</div>

我尝试了一些方法,但没有任何帮助?

更新

function get_page(){
  $file = 'file.php';
  $str = file_get_contents($file);
  $str = html_entity_decode($str);
  return $str;
}
$output = get_page();
echo $output;//don't work

修复

function get_page(){
      $file = 'file.php';
      $str = file_get_contents($file);
      return $str;
    }
    $output = get_page();
    echo html_entity_decode($output);//works

【问题讨论】:

    标签: php character


    【解决方案1】:

    函数是htmlspecialchars_decode()

    注意,解码引号的函数需要指定$quote_style参数。

    【讨论】:

    • @Val 你得到什么结果?你用什么代码?你是这样使用它的:$string = htmlspecialchars_decode($string);,对吧?
    • 是的,$str = htmlspecialchars_decode($str,ENT_NOQUOTES); 仍然不起作用。我使用fwrite 将它保存到一个文件中,当我尝试将它输出到 html 时,它会以纯文本而不是 html 元素的形式出现,如果有帮助的话
    • @Val 您的问题很可能出在其他地方。尝试echo $str; 以查看输出
    • 嗯,你的权利...看我的更新,并尝试帮助我哪里出了问题。
    • 问题出在return $str
    【解决方案2】:

    【讨论】:

      【解决方案3】:
      $from = array('<', '>');
      $to = array('<', '>');
      $string = str_replace($from, $to, $string);
      

      【讨论】:

      • 我可能会在这里待一年,试图找出所有单个字符,其次这可能会减慢页面速度,
      【解决方案4】:
      htmlspecialchars_decode($string)
      

      【讨论】:

        【解决方案5】:

        用这个更好...

        <?php
        $text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
        echo strip_tags($text);
        echo "\n";
        // Allow <p> and <a>
        
        echo strip_tags($text, '<p><a>');
        ?>
        

        【讨论】:

          猜你喜欢
          • 2013-08-14
          • 1970-01-01
          • 1970-01-01
          • 2014-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多