如何在PHP中将html标签转换为字符串

时间:2014-10-13 09:18:04

标签: php html5

我正在尝试将html标签转换为php中的字符串。但输出是不显示实际的html标签,输出显示你好世界感谢观看,我想输出为实际的html标签,请帮我解决这个问题

 <?php
$tag="<movie service='craftsman-1.0'><body><stack><sequence><effect type='sliding' duration='5.0'><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Canyon_Chelly_Navajo.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Ha_long_bay.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Monument_Valley.jpg'/></effect><effect type='none'><video filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/footage.mov' audio='false'/></effect></sequence><text type='zone' align='center,center'>Hello World</text><audio filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/george_woods_lucky_one.mp3' skip='5.0'/></stack><text type='zone' align='center,center'> Thanks for watching!</text></body></movie>";


echo $tag;
?>

我想要这样的输出

<movie service='craftsman-1.0'><body><stack><sequence><effect type='sliding' duration='5.0'><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Canyon_Chelly_Navajo.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Ha_long_bay.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Monument_Valley.jpg'/></effect><effect type='none'><video filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/footage.mov' audio='false'/></effect></sequence><text type='zone' align='center,center'>Hello World</text><audio filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/george_woods_lucky_one.mp3' skip='5.0'/></stack><text type='zone' align='center,center'> Thanks for watching!</text></body></movie>

但输出显示如下

 hello world  thanks for watching

5 个答案:

答案 0 :(得分:2)

使用以下代码

<?php
$tag="<movie service='craftsman-1.0'><body><stack><sequence><effect type='sliding'  duration='5.0'><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Canyon_Chelly_Navajo.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Ha_long_bay.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Monument_Valley.jpg'/></effect><effect type='none'><video filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/footage.mov' audio='false'/></effect></sequence><text type='zone' align='center,center'>Hello World</text><audio filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/george_woods_lucky_one.mp3' skip='5.0'/></stack><text type='zone' align='center,center'> Thanks for watching!</text></body></movie>";
echo htmlentities($tag);

&GT;

答案 1 :(得分:2)

使用strip_tags()它实现我们所期望的实际输出,字符串 strip_tags ( string $str [, string $allowable_tags ] ) 此函数尝试返回一个字符串,其中包含从给定str中剥离的所有NULL字节,HTML和PHP标记。它使用与fgetss()函数相同的标签剥离状态机。

参数

str: 输入字符串。

allowable_tags: 您可以使用可选的第二个参数来指定不应剥离的标记。

$tag=" <movie service='craftsman-1.0'><body><stack><sequence><effect type='sliding'  duration='5.0'><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Canyon_Chelly_Navajo.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Ha_long_bay.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Monument_Valley.jpg'/></effect><effect type='none'><video filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/footage.mov' audio='false'/></effect></sequence><text type='zone' align='center,center'>Hello World</text><audio filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/george_woods_lucky_one.mp3' skip='5.0'/></stack><text type='zone' align='center,center'> Thanks for watching!</text></body></movie>";
    echo strip_tags($tag);
    echo "\n";
    echo strip_tags($tag, '<movie><body><stack><sequence><effect><image><video><text><audio>');

输出

<movie service='craftsman-1.0'><body><stack><sequence><effect type='sliding'  duration='5.0'><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Canyon_Chelly_Navajo.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Ha_long_bay.jpg'/><image filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/Monument_Valley.jpg'/></effect><effect type='none'><video filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/footage.mov' audio='false'/></effect></sequence><text type='zone' align='center,center'>Hello World</text><audio filename='http://s3.amazonaws.com/stupeflix-assets/apiusecase/george_woods_lucky_one.mp3' skip='5.0'/></stack><text type='zone' align='center,center'> Thanks for watching!</text></body></movie>

答案 2 :(得分:1)

 $tag="<movie service='craftsman-1.0'><body>..... </body></movie>"; //use quotes to enclose the string but make sure you don't have any double quotes in the string

这个长期heredoc的另一个字符串解决方案

$tag = <<<EOT
    <movie service='craftsman-1.0'><body>
    ......
    </body></movie>
EOT;

答案 3 :(得分:0)

您的PHP变量需要用双引号括起来,如下所示:

$tag = "<movie service='etc...'>";

确保双引号内的所有HTML标记都使用单引号,否则它将结束PHP变量。 (你的例子似乎很好)

最后也不要忘记你的分号!

;

至于使回显打印出实际的HTML标记,您需要使用PHP函数htmlentities

echo htmlentities($tag);

答案 4 :(得分:0)

这应该......

echo htmlspecialchars($tag);