substr和mb_substr什么都不返回

时间:2016-05-01 20:23:02

标签: php substr

我不知道下面的代码有什么问题:

<?php
    $html = file_get_contents('https://www.ibar.az/en/');
    $doc = new domDocument();

    $doc->loadHTML($html);
    $doc->preserveWhiteSpace = false;

    $ExchangePart = $doc->getElementsByTagName('li');

    /*for ($i=0; $i<=$ExchangePart->length; $i++) {
        echo $i . $ExchangePart->Item($i)->nodeValue . "<br>";
    }*/

    $C=$ExchangePart->Item(91)->nodeValue;
    var_dump ($C);
    $fff=mb_substr($C, 6, 2, 'UTF-8');
    echo $fff;
    ?>

我已经尝试了substr和mb_substr,但在两种情况下都是echo $ fff;没有回报。

有人能说出我做错了吗?

1 个答案:

答案 0 :(得分:2)

这是项目91节点:

<ul>
    <li>USD</li>
    <li>1.5072</li>
    <li>1.462</li>
    <li>1.5494</li>
    <li class="down"> </li>
</ul>

这是节点值:

¶
····························USD¶
································1.5072¶
································1.462¶
································1.5494¶
································•¶
····························

( · = space; • = nbsp )

substr( $C, 6, 2 )是一个由两个空格组成的字符串。

要更正检索所有值:

foreach( $ExchangePart->Item(91) as $node )
{
    if( trim($node->nodeValue) ) echo $node->nodeValue . '<br>';
}

否则,您可以替换所有节点值空间:

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