How to eval PHP when making a template - getting syntax error










0















I have an html file where I have a syntax like '@echosomething', then I want to load that html file with file_get_contents and replace the template keyword so that it would echo a variable but I am getting a syntax error.



Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1


Here is my html_file.html



<html>
<body>
@echovalue
</body>




And here is my php code



<?php
$html_file = file_get_contents( 'html_file.html' );

$value = "This is a test value!";
$str = '<?php echo $value; ?>';

$html_file = str_replace('@echovalue', $str, $html_file);

eval($html_file);
echo $html_file;
?>









share|improve this question



















  • 1





    You're missing a ; on $value = "This is a test value!"

    – Nick
    Nov 12 '18 at 12:19











  • getting Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

    – Liga
    Nov 12 '18 at 12:19











  • How about escaping special characters? eval(htmlspecialchars($html_file));

    – Haris
    Nov 12 '18 at 12:24











  • now I'm getting Parse error: syntax error, unexpected '&', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

    – Liga
    Nov 12 '18 at 12:24






  • 2





    You would be better off (IMHO) looking into a proper template library rather than trying to make something of your own.

    – Nigel Ren
    Nov 12 '18 at 12:35















0















I have an html file where I have a syntax like '@echosomething', then I want to load that html file with file_get_contents and replace the template keyword so that it would echo a variable but I am getting a syntax error.



Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1


Here is my html_file.html



<html>
<body>
@echovalue
</body>




And here is my php code



<?php
$html_file = file_get_contents( 'html_file.html' );

$value = "This is a test value!";
$str = '<?php echo $value; ?>';

$html_file = str_replace('@echovalue', $str, $html_file);

eval($html_file);
echo $html_file;
?>









share|improve this question



















  • 1





    You're missing a ; on $value = "This is a test value!"

    – Nick
    Nov 12 '18 at 12:19











  • getting Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

    – Liga
    Nov 12 '18 at 12:19











  • How about escaping special characters? eval(htmlspecialchars($html_file));

    – Haris
    Nov 12 '18 at 12:24











  • now I'm getting Parse error: syntax error, unexpected '&', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

    – Liga
    Nov 12 '18 at 12:24






  • 2





    You would be better off (IMHO) looking into a proper template library rather than trying to make something of your own.

    – Nigel Ren
    Nov 12 '18 at 12:35













0












0








0








I have an html file where I have a syntax like '@echosomething', then I want to load that html file with file_get_contents and replace the template keyword so that it would echo a variable but I am getting a syntax error.



Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1


Here is my html_file.html



<html>
<body>
@echovalue
</body>




And here is my php code



<?php
$html_file = file_get_contents( 'html_file.html' );

$value = "This is a test value!";
$str = '<?php echo $value; ?>';

$html_file = str_replace('@echovalue', $str, $html_file);

eval($html_file);
echo $html_file;
?>









share|improve this question
















I have an html file where I have a syntax like '@echosomething', then I want to load that html file with file_get_contents and replace the template keyword so that it would echo a variable but I am getting a syntax error.



Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1


Here is my html_file.html



<html>
<body>
@echovalue
</body>




And here is my php code



<?php
$html_file = file_get_contents( 'html_file.html' );

$value = "This is a test value!";
$str = '<?php echo $value; ?>';

$html_file = str_replace('@echovalue', $str, $html_file);

eval($html_file);
echo $html_file;
?>






php templates eval






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 12:20







Liga

















asked Nov 12 '18 at 12:17









LigaLiga

1219




1219







  • 1





    You're missing a ; on $value = "This is a test value!"

    – Nick
    Nov 12 '18 at 12:19











  • getting Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

    – Liga
    Nov 12 '18 at 12:19











  • How about escaping special characters? eval(htmlspecialchars($html_file));

    – Haris
    Nov 12 '18 at 12:24











  • now I'm getting Parse error: syntax error, unexpected '&', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

    – Liga
    Nov 12 '18 at 12:24






  • 2





    You would be better off (IMHO) looking into a proper template library rather than trying to make something of your own.

    – Nigel Ren
    Nov 12 '18 at 12:35












  • 1





    You're missing a ; on $value = "This is a test value!"

    – Nick
    Nov 12 '18 at 12:19











  • getting Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

    – Liga
    Nov 12 '18 at 12:19











  • How about escaping special characters? eval(htmlspecialchars($html_file));

    – Haris
    Nov 12 '18 at 12:24











  • now I'm getting Parse error: syntax error, unexpected '&', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

    – Liga
    Nov 12 '18 at 12:24






  • 2





    You would be better off (IMHO) looking into a proper template library rather than trying to make something of your own.

    – Nigel Ren
    Nov 12 '18 at 12:35







1




1





You're missing a ; on $value = "This is a test value!"

– Nick
Nov 12 '18 at 12:19





You're missing a ; on $value = "This is a test value!"

– Nick
Nov 12 '18 at 12:19













getting Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

– Liga
Nov 12 '18 at 12:19





getting Parse error: syntax error, unexpected '<', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

– Liga
Nov 12 '18 at 12:19













How about escaping special characters? eval(htmlspecialchars($html_file));

– Haris
Nov 12 '18 at 12:24





How about escaping special characters? eval(htmlspecialchars($html_file));

– Haris
Nov 12 '18 at 12:24













now I'm getting Parse error: syntax error, unexpected '&', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

– Liga
Nov 12 '18 at 12:24





now I'm getting Parse error: syntax error, unexpected '&', expecting end of file in C:xampphtdocstemplatingindex.php(9) : eval()'d code on line 1

– Liga
Nov 12 '18 at 12:24




2




2





You would be better off (IMHO) looking into a proper template library rather than trying to make something of your own.

– Nigel Ren
Nov 12 '18 at 12:35





You would be better off (IMHO) looking into a proper template library rather than trying to make something of your own.

– Nigel Ren
Nov 12 '18 at 12:35












2 Answers
2






active

oldest

votes


















0














why not simply



$html_file = str_replace('@echovalue', $value, $html_file);


?






share|improve this answer























  • how would you do a foreach loop with this?

    – Liga
    Nov 12 '18 at 12:27











  • i'm not sure if i understand your question correctly, what loop do you need?

    – Lammi
    Nov 12 '18 at 12:49


















0














$html_file = file_get_contents( 'html_file.html' );

$value = "This is a test value!";
$str = 'return $value;';

echo str_replace('@echovalue', eval($str), $html_file);
echo $html_file;





share|improve this answer

























  • stilll getting the same erorr..

    – Liga
    Nov 12 '18 at 12:23






  • 1





    The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

    – Yaroslaw
    Nov 12 '18 at 12:25










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262041%2fhow-to-eval-php-when-making-a-template-getting-syntax-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














why not simply



$html_file = str_replace('@echovalue', $value, $html_file);


?






share|improve this answer























  • how would you do a foreach loop with this?

    – Liga
    Nov 12 '18 at 12:27











  • i'm not sure if i understand your question correctly, what loop do you need?

    – Lammi
    Nov 12 '18 at 12:49















0














why not simply



$html_file = str_replace('@echovalue', $value, $html_file);


?






share|improve this answer























  • how would you do a foreach loop with this?

    – Liga
    Nov 12 '18 at 12:27











  • i'm not sure if i understand your question correctly, what loop do you need?

    – Lammi
    Nov 12 '18 at 12:49













0












0








0







why not simply



$html_file = str_replace('@echovalue', $value, $html_file);


?






share|improve this answer













why not simply



$html_file = str_replace('@echovalue', $value, $html_file);


?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 12:27









LammiLammi

276




276












  • how would you do a foreach loop with this?

    – Liga
    Nov 12 '18 at 12:27











  • i'm not sure if i understand your question correctly, what loop do you need?

    – Lammi
    Nov 12 '18 at 12:49

















  • how would you do a foreach loop with this?

    – Liga
    Nov 12 '18 at 12:27











  • i'm not sure if i understand your question correctly, what loop do you need?

    – Lammi
    Nov 12 '18 at 12:49
















how would you do a foreach loop with this?

– Liga
Nov 12 '18 at 12:27





how would you do a foreach loop with this?

– Liga
Nov 12 '18 at 12:27













i'm not sure if i understand your question correctly, what loop do you need?

– Lammi
Nov 12 '18 at 12:49





i'm not sure if i understand your question correctly, what loop do you need?

– Lammi
Nov 12 '18 at 12:49













0














$html_file = file_get_contents( 'html_file.html' );

$value = "This is a test value!";
$str = 'return $value;';

echo str_replace('@echovalue', eval($str), $html_file);
echo $html_file;





share|improve this answer

























  • stilll getting the same erorr..

    – Liga
    Nov 12 '18 at 12:23






  • 1





    The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

    – Yaroslaw
    Nov 12 '18 at 12:25















0














$html_file = file_get_contents( 'html_file.html' );

$value = "This is a test value!";
$str = 'return $value;';

echo str_replace('@echovalue', eval($str), $html_file);
echo $html_file;





share|improve this answer

























  • stilll getting the same erorr..

    – Liga
    Nov 12 '18 at 12:23






  • 1





    The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

    – Yaroslaw
    Nov 12 '18 at 12:25













0












0








0







$html_file = file_get_contents( 'html_file.html' );

$value = "This is a test value!";
$str = 'return $value;';

echo str_replace('@echovalue', eval($str), $html_file);
echo $html_file;





share|improve this answer















$html_file = file_get_contents( 'html_file.html' );

$value = "This is a test value!";
$str = 'return $value;';

echo str_replace('@echovalue', eval($str), $html_file);
echo $html_file;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 12:42

























answered Nov 12 '18 at 12:20









YaroslawYaroslaw

834




834












  • stilll getting the same erorr..

    – Liga
    Nov 12 '18 at 12:23






  • 1





    The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

    – Yaroslaw
    Nov 12 '18 at 12:25

















  • stilll getting the same erorr..

    – Liga
    Nov 12 '18 at 12:23






  • 1





    The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

    – Yaroslaw
    Nov 12 '18 at 12:25
















stilll getting the same erorr..

– Liga
Nov 12 '18 at 12:23





stilll getting the same erorr..

– Liga
Nov 12 '18 at 12:23




1




1





The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

– Yaroslaw
Nov 12 '18 at 12:25





The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

– Yaroslaw
Nov 12 '18 at 12:25

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262041%2fhow-to-eval-php-when-making-a-template-getting-syntax-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20