我想删除重复的``` ```如果它们是连续的,但是我的条件“ if($ value ===” “)”不起作用

时间:2019-09-24 11:04:11

标签: php

字符串:       373704  02.01.18            719,19     381057  13.01.18              0,00

我的功能:

 public function removeDuplicateNBSP($string){
        $textArray = explode(" ", $string);
        foreach ($textArray as $key => $value) {
            if($value === " " && $textArray[key+1] === " "){
                unset($textArray[$key]);
                array_values($textArray);
                print_r($value);
                echo "</br>";
            }
        } 
        $arrayToText = implode(" ", $textArray);
        return $arrayToText;
    }

预期结果:&nbsp;373704 &nbsp;02.01.18 &nbsp;719,19 &nbsp;381057 &nbsp;13.01.18 &nbsp;0,00

我想要的是:如果重复的&nbsp;是连续的,但我的条件不起作用,我想删除它们。我该怎么办?

1 个答案:

答案 0 :(得分:0)

只需过滤所有&nbsp;值:

$string = '&nbsp; &nbsp; &nbsp; &nbsp;373704 &nbsp;02.01.18 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;719,19 &nbsp; &nbsp; 381057 &nbsp;13.01.18 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,00';     

$values = explode(' ', $string);
$func = function($val) {
    return $val !== '&nbsp;';
};

$filtered = implode(' ', array_filter($values, $func));

echo $filtered;