douwu7168 2015-01-16 14:45
浏览 62
已采纳

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

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>
  • 写回答

3条回答 默认 最新

  • dongzou9047 2015-01-16 14:50
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3