Rat2good
Rat2good

Reputation: 119

substr and mb_substr return nothing

I do not know what is wrong in the code below:

<?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;
    ?>

I have tried both substr and mb_substr but in both cases echo $fff; returns nothing.

Could anybody suggest what I am doing wrong?

Upvotes: 0

Views: 257

Answers (1)

fusion3k
fusion3k

Reputation: 11689

This is the item 91 node:

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

This is node value:

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

( · = space; • = nbsp )

substr( $C, 6, 2 ) is a string of two spaces.

To correct retrieve all values:

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

Otherwise, you can replace all node value spaces:

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

Upvotes: 2

Related Questions