Taha Rushain
Taha Rushain

Reputation: 635

Formating Html code as a string in php for printf

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>

Upvotes: 0

Views: 4448

Answers (3)

Anoop S
Anoop S

Reputation: 141

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;

Upvotes: 1

Forien
Forien

Reputation: 2763

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;

Upvotes: 1

Robert
Robert

Reputation: 2824

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>

Upvotes: 1

Related Questions