0

I have a full HTML page:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Template</title>
    <meta name="description" content="">
    <meta name="HandheldFriendly" content="True">
  ...
</head>
<body>
...
</body>
</html>

I'm trying to save it in a variable like so:

$template = htmlentities("<!DOCTYPE HTML><html lang="en-US">...", ENT_HTML5, "UTF-8" );

.. but it chokes at just the first HTML tag.

1
  • Err, use heredoc? Commented Feb 6, 2015 at 6:48

5 Answers 5

4

That's because the first HTML tag has double quotes, just like you use for delimiting your string literal.

$template = <<<EOD
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Template</title>
    <meta name="description" content="">
    <meta name="HandheldFriendly" content="True">
  ...
</head>
<body>
...
</body>
</html>
EOD;
Sign up to request clarification or add additional context in comments.

3 Comments

Would just add a note for beginners like me that ending EOD; must not have empty space before it.
btw, it doesn't seem to recognize others variables inside the EOD, for example <p class="meta">►►► BRAND NEW ◉ <?= $description ?></p>
You wouldn't use a short echo tag in a string literal, so you wouldn't use it in a heredoc either.
0

You are not escaping the string propertly Try to: Replace

htmlentities("//whatever your html code is//");

with

htmlentities('//whatever your html code is//');

Comments

0

user addslashes function..it will not truncate your string in between. This function can be used to prepare a string for storage in a database and database queries.

Before storing into database or for any purpose

$final_string = addslashes('<!DOCTYPE HTML>

..........');

Before rendering that output on browser

$normal_string = stripslashes($database_retrived_string);

6 Comments

"This function can be used to prepare a string for storage in a database and database queries." - No, it can't.
Preparing a string for database requires either a dedicated database escaping function or prepared statements; preparing a string for output in a browser requires htmlspecialchars().
Why are you assuming that it's going to be stored in a database?
I have given example...we can use it for any purpose...we can use that function to storing into variable without any break and can use stripslashes to render it again...
|
0
$data = '<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Template</title>
    <meta name="description" content="">
    <meta name="HandheldFriendly" content="True">
  ...
</head>
<body>
...
</body>
</html>';

base64_encode($data);

1 Comment

He would still have to manually escape all of the single quotes. Heredoc works. Not sure why he wouldn't just read the file in though...
0

Try this:

$temp     = addslashes('<!DOCTYPE HTML><html lang="en-US">...', ENT_HTML5, "UTF-8" );

$template = htmlentities($temp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.