如何在php变量中存储html进行打印?

时间:2014-02-24 09:57:38

标签: php html

我想将html存储在$var之类的变量中。如何在php变量中存储这个html?

这是我的代码:

 $var="<div id=\"w1\" style=\"width:136px; height:28px; background:{$color}; float:left\"><p style=\"margin-top:6px; padding-left:40px;\">{$date=$row['entered']} {date('F', strtotime($date))}</p></div>                                                                <div id="w2" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:30px;"><?php if($basic==''){ echo 0; } else { echo $basic; } ?></p></div>                               
                      <div id="w3" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:30px;"><?php if($silver==''){ echo 0; } else { echo $silver; } ?></p></div>                         
                      <div id="w4" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:30px;"><?php if($gold==''){ echo 0; } else {echo $gold; } ?></p></div>                         
                      <div id="w5" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:30px;"><?php if($platinum==''){ echo 0; } else {echo $platinum; } ?></p></div>                         
                      <div id="w6" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"></div>                         
                      <div id="w7" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px;"></p></div>                         
                      <div id="w8" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:20px;"><?php echo ($row['credit']); ?></p></div>                         
                      <div id="w9" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"></div>                         
                      <div id="w10" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"></div>
                     <div id="w10" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="padding-left:20px; margin-top:6px;"><?php if($row['article']!=''){ echo 'A';} if($row['event']!=''){echo 'E'; } ?></p></div>                         <div id="w11" style="width:79px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"></div>                         
                     <div id="w12" style="width:109px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"></div>

4 个答案:

答案 0 :(得分:2)

您可以使用HEREDOC

php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

$var = <<<MYSTRING
<div>My Div Tag</div>
<p>My Paragraph Tag</p>
MYSTRING;

答案 1 :(得分:0)

您可以使用ob_start()ob_get_clean()缓冲输出,以便日后重复使用。

<?php
ob_start(); ?>
<div><p>My HTML</p></div>
<?php $var = ob_get_clean(); ?>

答案 2 :(得分:0)

像这样使用ob_start()和ob_get_contents()

<?php
   ob_start()
   $var = '<html>------</html>';
   $html = ob_get_contents();
   ob_end_clean();
   echo $html;
?>

答案 3 :(得分:0)

尝试类似:

<?php
ob_start();
?>

<div id=\"w1\" style=\"width:136px; height:28px; background:{$color}; float:left\"><p style=\"margin-top:6px; padding-left:40px;\">{$date=$row['entered']} {date('F', strtotime($date))}</p></div>                                                                <div id="w2" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:30px;"><?php if($basic==''){ echo 0; } else { echo $basic; } ?></p></div>                               
                      <div id="w3" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:30px;"><?php if($silver==''){ echo 0; } else { echo $silver; } ?></p></div>                         
                      <div id="w4" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:30px;"><?php if($gold==''){ echo 0; } else {echo $gold; } ?></p></div>                         
                      <div id="w5" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:30px;"><?php if($platinum==''){ echo 0; } else {echo $platinum; } ?></p></div>                         
                      <div id="w6" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"></div>                         
                      <div id="w7" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px;"></p></div>                         
                      <div id="w8" style="width:69px; height:28px; float:left;margin:0 0 0 1px;  background:<?php echo $color; ?>"><p style="margin-top:6px; padding-left:20px;"><?php echo ($row['credit']); ?></p></div>
<?php
$html=ob_get_contents();
ob_end_clean();
echo $html;
?>