Reputation: 538
i am trying convert html tag into string in php. but output is does not display actual html tag , output display hello world thanks for watching, i want output as actual html tag , please help me to resolve this problem
<?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;
?>
i want output like this
<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>
but output display like this
hello world thanks for watching
Upvotes: 3
Views: 32129
Reputation: 538
use the strip_tags()it delivery actual output what we expected ,string strip_tags ( string $str [, string $allowable_tags ] )
This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given str. It uses the same tag stripping state machine as the fgetss() function.
Parameters
str : The input string.
allowable_tags : You can use the optional second parameter to specify tags which should not be stripped.
$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>');
the output is
<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>
Upvotes: 3
Reputation: 477
Your PHP variable needs to be surrounded in double quotes, like this:
$tag = "<movie service='etc...'>";
Just make sure that all of your HTML tags inside of the double quotes are using single quotes, otherwise it will end the PHP variable. (Your example seems to be fine)
Also don't forget your semi-colon at the end!
;
As for making the echo print out the actual HTML tags, you need to use the PHP function htmlentities.
echo htmlentities($tag);
Upvotes: 1
Reputation: 159
$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
another solution for strings this long heredoc
$tag = <<<EOT
<movie service='craftsman-1.0'><body>
......
</body></movie>
EOT;
Upvotes: 1
Reputation: 423
Use the following code
<?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);
?>
Upvotes: 3