在PHP中为printf格式化Html代码作为字符串

时间:2015-01-16 14:45:25

标签: php html

Hello Stackoverflow社区,

我是php的新手,我知道我现在必须做一些非常愚蠢的事情。

我遇到了一个问题,将HTML代码格式化为PHP中的字符串,用于printf函数。我基本上希望我的php printf函数使用printf函数打印HTML代码。

我已经看到了其他可能的重复问题并且已经付出了努力,但我仍然坚持这个问题。

我尝试用转义序列替换一些特殊字符,但问题仍然存在。

示例:

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

生成错误的代码:

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");    

错误讯息:

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

实际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>

3 个答案:

答案 0 :(得分:1)

html里面的php

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

或php里面的html

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

答案 1 :(得分:1)

为什么使用printf()?我会将echoHEREDOC一起使用。

像:

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;

答案 2 :(得分:1)

您确定需要使用printf()吗?

请尝试使用echoheredoc

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;