dsaba Posted April 3, 2007 Share Posted April 3, 2007 hey I am using this line of code to write to a file, I'm trying to create a php file I need to escape the php tags, how do I do that? I tried doing this: <?php $startphp = "<?php"; $closephp = "?>"; $htmlforfile = <<<HTMLFORFILE $startphp session_start(); if ($_SESSION['translation'] != "jamba-translation") { die("not allowed"); } $closephp <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body>$text</body></html> HTMLFORFILE; ?> as you can see i'm using heredoc syntax because i'm using this one variable like this: fwrite($handle, $htmlforfile) or die("could not write to the file"); $htmlforfile is the heredoc variable i created you see the general idea is that I want to write to a file and let it be a php file How do I do this? -thanks Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 3, 2007 Share Posted April 3, 2007 strip_tags (PHP 4, PHP 5) strip_tags — Strip HTML and PHP tags from a string Description string strip_tags ( string $str [, string $allowable_tags] ) Quote Link to comment Share on other sites More sharing options...
dsaba Posted April 3, 2007 Author Share Posted April 3, 2007 I dont want to strip tags, i just want to escape the tags so I can write them in a php script without the php script acting like I want to use them for example if I write ?> it will end the php script if i escape_tags(?>) then it will escape the tags get it? its like escaping quotes, doesn't strip them just lets u use them the problem I DONT KNOW OF ANY ESCAPE TAGS function maybe there is another method to do this if there is no such function? -thanks Quote Link to comment Share on other sites More sharing options...
btherl Posted April 3, 2007 Share Posted April 3, 2007 Heredoc syntax will escape everything.. like so <?php $var = <<<HEREDOC <?php blah ?> HEREDOC; print $var; ?> Quote Link to comment Share on other sites More sharing options...
dsaba Posted April 3, 2007 Author Share Posted April 3, 2007 I tried that, that will not work Quote Link to comment Share on other sites More sharing options...
dsaba Posted April 3, 2007 Author Share Posted April 3, 2007 this code gives me a parse error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING $htmlforfile = <<<HTMLFORFILE <?php session_start(); if ($_SESSION['translation'] != "jamba-translation") { die("not allowed"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body>$text</body></html>' HTMLFORFILE; if there is nothing wrong with this code, then that means that the HEREDOC did not escape the php tags its also obvious because in my dreamweaver highlighting its all wrong, as if ITS NOT ESCAPING TAGS Quote Link to comment Share on other sites More sharing options...
btherl Posted April 3, 2007 Share Posted April 3, 2007 Oh.. I see. It's not the php tags that are your problem. The heredoc syntax is not escaping php variables, that is what is causing your problem. Try this: <?php $htmlforfile = <<<HEREDOC <?php session_start(); if (\$_SESSION['translation'] != "jamba-translation") { die("not allowed"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body>\$text</body></html>' HEREDOC; print $htmlforfile; ?> The difference is that I have escaped the "$" signs. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.