PHP trim()不能很好地工作

时间:2013-10-09 10:32:57

标签: php

trim()不起作用,我不明白为什么。我添加模型并不重要,对不起第一次添加。

当我运行它并从Excel加载123 123 123行时,这不会让我123123123

我该如何解决?

如果您需要该型号,请发表评论。

PHP有点控制器

class Kontroler extends CI_Controller {

    public function upload()
    {
        $file = 'przyklad kk.xls';
        $this->load->library('excel');
        $this->excel=PHPExcel_IOFactory::load($file);  
        $this->excel->setActiveSheetIndex(0);
        $array = $this->excel->getActiveSheet()->toArray(null, true, true, true);

        for ($i = 2; $i <= count($array); $i++) {
            if ($array[$i]['A'] == NULL) $array[$i]['A'] = 'Brak';
            if ($array[$i]['B'] == NULL) $array[$i]['B'] = 'Brak';
            if ($array[$i]['C'] == NULL) $array[$i]['C'] = '00-000';
            if ($array[$i]['F'] == NULL) $array[$i]['F'] = 'Brak';
            if ($array[$i]['G'] == NULL) $array[$i]['G'] = 'Brak';
            if ($array[$i]['D'] == NULL) $array[$i]['D'] = 'Brak';
            if ($array[$i]['H'] == NULL) $array[$i]['H'] = 'Brak';
            if ($array[$i]['E'] == NULL) $array[$i]['E'] = 'Brak';
            if ($array[$i]['J'] == NULL) $array[$i]['J'] = 'Brak';
            if ($array[$i]['I'] == NULL) $array[$i]['I'] = 'brak@brak.pl';
            if ($array[$i]['N'] == NULL) $array[$i]['N'] = 'Brak';

            $tab[$i]['City'] = $array[$i]['D'];
            $tab[$i]['ContactPersonName'] = $array[$i]['F'];
            $tab[$i]['ContactPersonSubname'] = $array[$i]['G'];
            $tab[$i]['Email'] = $array[$i]['I'];
            $tab[$i]['Name'] = $array[$i]['A'];
            $tab[$i]['Phone'] = $array[$i]['H'];
            $tab[$i]['PostCode'] = $array[$i]['C'];
            $tab[$i]['Potential'] = $array[$i]['J'];
            $tab[$i]['Powiat'] = $array[$i]['E'];
            $tab[$i]['Street'] = $array[$i]['B'];
            $tab[$i]['ID_GR'] = $array[$i]['L'];
            $tab[$i]['ID_RM'] = $array[$i]['M'];
            $tab[$i]['ID_PR'] = $array[$i]['N'];    

            $Name = trim($array[$i]['A']);
            $Street = trim($array[$i]['B']);
            $PostCode = trim($array[$i]['C']);
            $City = trim($array[$i]['D']);
            $ContactPersonName = trim($array[$i]['F']);
            $ContactPersonSubname = trim($array[$i]['G']);
            $Email = trim($array[$i]['I']);
            $Name = trim($array[$i]['D']);
            $Phone = trim($tab[$i]['Phone']);
            $Powiat = trim($array[$i]['E']);
            $Potential = trim($array[$i]['J']);         
            $ID_GR = trim($tab[$i]['ID_GR']);
            $ID_RM = trim($array[$iQ]['M']);
            $ID_PR = trim($array[$i]['N']);             
        }
        $this->model->insert_from_file_kk($tab);
    }

    public function show()
    {
        $data['rekordy'] = $this->model->get_rek();
        $this->load->view('widok', $data);
    }
}

PHP视图

<div id="content">
<table border=1>
<tr>
<td>Nazwa</td><td>Ulica</td><td>Kod pocztowy</td><td>Miasto</td><td>Powiat</td><td>Imie os kontaktowej</td><td>Nazwisko os kontaktowej</td><td>Telefon</td><td>Email</td>     <td>Potencjal</td>
<td>ID_PR</td><td>ID_GR</td><td>ID_RM</td>
</tr>
<?php

foreach ($rekordy as $rekord)
{
    echo "
    <tr>
    <td>$rekord->Name</td>
    <td>$rekord->Street</td>
    <td>$rekord->PostCode</td>
    <td>$rekord->City</td>
    <td>$rekord->Powiat</td>
    <td>$rekord->ContactPersonName</td>
    <td>$rekord->ContactPersonSubname</td>
    <td>$rekord->Phone</td>
    <td>$rekord->Email</td>
    <td>$rekord->Potential</td>
    <td>$rekord->ID_PR</td>
    <td>$rekord->ID_GR</td>
    <td>$rekord->ID_RM</td>
    </tr>
";

}

?>
</table>
</div>

2 个答案:

答案 0 :(得分:4)

  

当我运行它并从excel加载行123 123 123时,这不会让我123123123。

这就是trim($str)应该做的事情:从$str的开头和结尾剥去空格。注意:从开头到结尾只有空格。请参阅the documentation

要实现您想要的效果,只需用空字符串替换所有空格:

$subject = "123 123 123";
$output = str_replace(" ", "", $subject);
echo $output;

输出

  

123123123

答案 1 :(得分:1)

我可能会离开这里,但看起来您忘记将PHP代码包装在PHP标记中:

<td><?php echo $rekord->Name; ?></td>
<td><?php echo $rekord->Street; ?></td>
<td><?php echo $rekord->PostCode; ?></td>