php - 根据条件插入html标签

时间:2013-10-20 08:07:26

标签: php html

使用php我希望根据条件是否满足来为某些内容包装div标签。目前我正在执行此操作,如下例所示。有没有更好的方法呢?它似乎不是很专业。

<?php if($Subject) echo "<div id='someID'>";?>

    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
 Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
 printer took a galley of type and scrambled it to make a type specimen book. It has 
survived not only five centuries, but also the leap into electronic typesetting, remaining
 essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets
 containing Lorem Ipsum passages, and more recently with desktop publishing software like
 Aldus PageMaker including versions of Lorem Ipsum.</p>

<?php if($Subject) echo "</div>";?>

2 个答案:

答案 0 :(得分:0)

你可以通过隐藏div

来做这样的事情
  <div id="someID" <?php if (!$Subject){?>style="display:none"<?php } ?>>

答案 1 :(得分:0)

很难假设你的问题的整个上下文(最好再写一点$subject的值来自何处以及它可能是什么值。)

但假设$subject可以是一组指定的值,您可以尝试这样的事情:

$text = 'Some arbitrary text';

$allowedWrappers = array(
    'one'   => 'someID',
    'two'   => 'anotherID',
    'three' => 'yetAnotherID'
);

if(isset($subject) && in_array($subject, $allowedWrappers)) {
    echo '<div id="'.$allowedWrappers[$subject].'"><p>'.$text.'</p></div>';
} else {
    echo '<p>'.$text.'</p>';
}