如何删除td标签中的空格?

时间:2014-08-21 12:50:44

标签: php html html-table removing-whitespace

我有一个html表,我在复杂的逻辑中填写php。 有4个foreach循环来填充数据。

现在这个工作正常,但通过循环,我的元素中有一些空格。 浏览器可以很好地显示它,但是一旦我想将它导出为pdf,就会出现一些问题。

任何人都知道如何解雇这些空白。请查看图片以获得更多说明:

这是代码我的变量,我回应出来没有任何空格。如果给出echo“test”;

,也会出现问题
<div id="autoload-content"> 

    <table class="tg" >
      <thead>
          <tr>
            <th></th>
            <th>Montag</th>
            <th>Dienstag</th>
            <th>Mittwoch</th>
            <th>Donnerstag</th>
            <th>Freitag</th>
          </tr>
      </thead>
      <tbody>

    <?php
        $arrNoDoubleEntries = array();
        $totalFactorMo = 0;
        $stackIrregular = array();

        //displays all the different presencetypes
        foreach ($types as &$value) {
        ?>    
         <tr>
            <td><?php echo ($value->Name);?></td>

            <td>
            <?php //all Monday Childs 

            if ($value->Name == "Total"){
                echo ($totalFactorMo);
            }else{
                $childrenMonday = (Children::LoadPresence(1, $groupId));
                    foreach ($childrenMonday as &$child) {
                            foreach ($irregularPresence as &$irrugPrese){

                                if (($child->id == $irrugPrese->child_id) && ($monday >= $irrugPrese->datefrom) && ($monday <= $irrugPrese->dateto)){
                                    if ($irrugPrese->away == 1 && ($value->id == $child->presencetype)){                                
                                            echo("<s>".$child->fullName . " " . $child->factor."</s>");
                                            echo "<br />";
                                            array_push($stackIrregular, $child->id);
                                    }

                                    else{   

                                            if (($value->id == $irrugPrese->presencetype_id) AND (!(in_array($child->fullName + $irrugPrese->presencetype_id, $arrNoDoubleEntries)))){
                                                echo("<u>".$child->fullName . " " . $child->factor."</u>");
                                                echo "<br />";
                                                array_push($arrNoDoubleEntries, $child->fullName + $irrugPrese->presencetype_id);
                                                $totalFactorMo = $totalFactorMo + $child->factor;
                                            }
                                    }
                                }
                            }

                        if (!(in_array($child->id, $stackIrregular))) {
                            //prints the children which dont have some irregular Presences
                            if (!($child->presencetype == 1) && ($value->id == $child->presencetype)){
                                echo ($child->fullName . " " . $child->factor);
                                echo "<br />";
                                $totalFactorMo = $totalFactorMo + $child->factor;
                            }
                        }
                    }
            }

            ?>
            </td>
        </tr>
        </tbody>
    </table>
</div>

3 个答案:

答案 0 :(得分:0)

您可以在显示表格之前修剪变量中的空格,如

$string = preg_replace('/\s+/', '', $string);

$string = str_replace(' ', '', $string);

答案 1 :(得分:0)

在打印值之前,只需剥离所有空白区域。

EG:

function cleanSpaces($text)  
{
  $text = preg_replace('/(xC2xA0/|&nbsp;)','',$text); //remove &nbsp;
  $text = preg_replace('/\s+/', '', $loop_variable_value); //remove whitespace
  return $text;
}

答案 2 :(得分:0)

在你的情况下尝试连接html,然后显示它像

<?php //all Monday Childs 
    $html = '';
        if ($value->Name == "Total"){
            $html .= ($totalFactorMo);
        }else{
            $childrenMonday = (Children::LoadPresence(1, $groupId));
                foreach ($childrenMonday as &$child) {
                        foreach ($irregularPresence as &$irrugPrese){

                            if (($child->id == $irrugPrese->child_id) && ($monday >= $irrugPrese->datefrom) && ($monday <= $irrugPrese->dateto)){
                                if ($irrugPrese->away == 1 && ($value->id == $child->presencetype)){                                
                                        $html .=("<s>".$child->fullName . " " . $child->factor."</s>");
                                        $html .= "<br />";
                                        array_push($stackIrregular, $child->id);
                                }

                                else{   

                                        if (($value->id == $irrugPrese->presencetype_id) AND (!(in_array($child->fullName + $irrugPrese->presencetype_id, $arrNoDoubleEntries)))){
                                            $html .=("<u>".$child->fullName . " " . $child->factor."</u>");
                                            $html .= "<br />";
                                            array_push($arrNoDoubleEntries, $child->fullName + $irrugPrese->presencetype_id);
                                            $totalFactorMo = $totalFactorMo + $child->factor;
                                        }
                                }
                            }
                        }

                    if (!(in_array($child->id, $stackIrregular))) {
                        //prints the children which dont have some irregular Presences
                        if (!($child->presencetype == 1) && ($value->id == $child->presencetype)){
                            $html .= ($child->fullName . " " . $child->factor);
                            $html .= "<br />";
                            $totalFactorMo = $totalFactorMo + $child->factor;
                        }
                    }
                }
        }
        echo $html;

        ?>