简体   繁体   English

PHP:从字符串数组中删除“\ ”

[英]PHP: Remove "\u00a0" from array of strings

After parsing some html, we have an array similar to ["\ ","\ ", "foo", "\ ", "bar"] .解析一些 html 后,我们有一个类似于["\ ","\ ", "foo", "\ ", "bar"] I want to filter out the "\ " leaving us with ["foo", "bar"] .我想过滤掉 "\ " 给我们留下["foo", "bar"] However, when we run an equality check, we are not getting a match.然而,当我们运行相等检查时,我们没有得到匹配。

We have tried我们试过了

  • $item != "\ "
  • $item != "\\\ "
  • $item != ""
  • $item != " "
  • $item != chr(160)

We get the same array returned without the "\ " filtered out.我们得到了没有过滤掉“\ ”的相同数组。

function getContent($xPath) {
    $query = "//div[@class='WordSection1']";
    $elements = $xPath->query($query);
        
    if (!is_null($elements)) {
        $content = array();
        foreach ($elements as $element){
            $nodes = $element->childNodes;
            foreach ($nodes as $node) {
                if ($node->nodeValue != "\u00a0") {
                    $content[] = $node->nodeValue;
                }
            }
        }
        return $content;
    }
}

Try doing this condition:尝试执行此条件:

if ($node->nodeValue != "\u{00a0}") {
    $content[] = $node->nodeValue;
}

Here's the sample script I used:这是我使用的示例脚本:

<?php

$words = [chr(0xC2).chr(0xA0), 'foo', chr(0xC2).chr(0xA0), 'bar'];
$output = [];

foreach ($words as $word) {
    if ($word != "\u{00a0}")
        $output[] = $word;
}

var_dump($output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM