【问题标题】:how to remove white space in td tag?如何删除td标签中的空白?
【发布时间】:2014-08-21 21:20:18
【问题描述】:

我有一个 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>

【问题讨论】:

  • 显示生成表格的代码……
  • 为什么不尝试循环遍历表格单元格并使用javascript trim method?修剪空格
  • HTML 通常会忽略空格。你确定不加  或者像   这样的一些不可见的空间或将 CSS 设置为预先输入的空白​​处理

标签: php html html-table removing-whitespace


【解决方案1】:

在您的情况下,尝试连接 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;

        ?>

【讨论】:

  • 成功了!我给出的小修改: echo '' 。 $html 。 '';并删除了 php 周围的 标签
【解决方案2】:

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

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

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

【讨论】:

  • HTML 将忽略普通空格
  • 普通空格是什么意思?
  • %20 字符在 HTML 中大部分被忽略。
【解决方案3】:

在打印值之前去掉所有的空白。

EG:

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

【讨论】:

  • 这仅在字符串中有实际空格时才有效。如果它使用   存储在数据库中怎么办? ?
  • 我的变量中没有空格。我不知道这些空间是在哪里以及为什么创建的。如果我给出硬编码的东西也会发生这种情况。
  • 你能分享一下你的变量的值吗?
猜你喜欢
  • 2014-05-21
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多