0

Hello Stackoverflow Community,

I am new to php , I know I must be doing something very stupid at this point.

I am having an issue formatting html code as a string in PHP for printf function. I basically want my php printf function to print HTML code using printf function.

I have seen other possible duplicate questions and have made my efforts but I am still stuck with this issue.

I tried replacing some special characters with their escape sequence but the problem remains.

example:

< with "\074"
> with "\076"
. with "\056"
{ with "\173"
} with "\175"
: with "\072" 
= with "\075"
- with "\055"

Code that generates error :

printf("\074html\076"
."\074head\076"
."\074style\076"
."  \056outer\173display\072 table;"
."  position\072 absolute;"
."  height\072 100%;"
."  width\072 100%;\175"
."  \056inner\173display\072 table\055cell;"
."  vertical\055align\072 middle;"
."  background\055image\072none;"
."  border\072none;"
."  color\072#fff;"
."  letter\055spacing\0721px;"
."  font\055weight\072600;"
."  text\055decoration\072none;"
."  text\055shadow\072none;"
."  text\055transform\072 uppercase;"
."  text\055align\072 center;\175"
."\074/style\076"
."\074/head\076"
."\074body style\075\'background\072 #336E7B;\'\076"
."  \074div class\075\'outer\'\076"
."  \074div class\075 \'inner\'\076" 
."  \074p> Message Send\074/p\076"
."  \074p style\075\'color\072#E26A6A;\'>Redirecting Back\056\056\056\074/p\076"
."  \074/div\076"
."  \074/div\076"
."\074/body\076"
."\074/html\076");    

Error Message :

Warning: printf() [function.printf]: Too few arguments in     /home/a1846075/public_html/contact.php on line 101

Actual Html Code :

<html>
<head>
 <style>
  .outer{display: table;
    position: absolute;
    height: 100%;
    width: 100%;}
   .inner{display: table-cell;
    vertical-align: middle;
    background-image:none;
    border:none;
    color:#fff;
    letter-spacing:1px;
    font-weight:600;
    text-decoration:none;
    text-shadow:none;
    text-transform: uppercase;
    text-align: center;}
</style>
</head>
<body style='background: #336E7B;'>
 <div class='outer'>
    <div class= 'inner'> 
        <p> Message Send</p>
        <p style='color:#E26A6A;'>Redirecting Back...</p>
    </div>
 </div>
</body>
</html>
2
  • Use htmlspecialchars Commented Jan 16, 2015 at 14:46
  • @CasimiretHippolyte, Sir can you provide an example on how to use it? Commented Jan 16, 2015 at 14:49

3 Answers 3

1

html inside php

<?php
$test="test my page";
$page= <<< EOPAGE
<html>
<head>
<title>PHP</title>
</head>
<body>
$test
</body>
</html>
EOPAGE;
echo $page;
?>

or php inside html

<html>
<head>
<title>PHP</title>
</head>
<body>
<?php
$test="test my page";
echo $test;
?>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

1

Why do you use printf()? I would use echo with HEREDOC.

Like:

echo <<<DOC
<html>
My verry very very very
very
very

very very long html script with some <a href="link">links</a>
and stuff
</html>
DOC;

Comments

1

Are you sure that you need to use printf()?

Please try using echo and heredoc

echo <<<HTML
<html>
<head>
 <style>
  .outer{display: table;
    position: absolute;
    height: 100%;
    width: 100%;}
   .inner{display: table-cell;
    vertical-align: middle;
    background-image:none;
    border:none;
    color:#fff;
    letter-spacing:1px;
    font-weight:600;
    text-decoration:none;
    text-shadow:none;
    text-transform: uppercase;
    text-align: center;}
</style>
</head>
<body style='background: #336E7B;'>
 <div class='outer'>
    <div class= 'inner'> 
        <p> Message Send</p>
        <p style='color:#E26A6A;'>Redirecting Back...</p>
    </div>
 </div>
</body>
</html>
HTML;

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.