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

3

我正在尝试将HTML标签转换为字符串(string)在PHP中。但是输出结果并没有显示实际的HTML标签,输出结果只显示了“hello world,谢谢观看”,我希望输出结果是实际的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

你少了第一个引号... `$tag="<movie..." - Tim S
5个回答

3

使用strip_tags()函数可以得到我们期望的实际输出结果,即从给定字符串中删除所有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>

3
请使用以下代码。
<?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);

?>


1

您的PHP变量需要用双引号括起来,就像这样:

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

请确保双引号内的所有HTML标签都使用单引号,否则会结束PHP变量。(您的示例似乎没问题)

还要不要忘记在结尾加上分号!

;

关于让回声打印出实际的HTML标签,您需要使用PHP函数htmlentities
echo htmlentities($tag);

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;

1
这应该可以做到...
echo htmlspecialchars($tag);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接