Jump to content

escape <?php ?> tags


dsaba

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/45386-escape-tags/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/45386-escape-tags/#findComment-220360
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/45386-escape-tags/#findComment-220366
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/45386-escape-tags/#findComment-220377
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.