Rat2good
Rat2good

Reputation: 119

Getting rid off extra spaces using PHP. What is wrong with my code?

I am trying to retrieve some info from the site using PHP code below:

$doc = new domDocument();
$html = file_get_contents('https://www.ibar.az/en/');
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_use_internal_errors(false);
$ExchangePart = $doc->getElementsByTagName('li');
$USD=$ExchangePart->Item(91)->nodeValue;

$USD=htmlentities($USD);
$USD=str_replace("&nbsp","", $USD);

$TrimedUSD=trim($USD);

$USDArray=str_split($TrimedUSD);
$TotalSymbols=count($USDArray);
$k=0;

for ($i=0; $i<$TotalSymbols; $i++) {
    //var_dump($USDArray[$i]);
    if ($USDArray[$i]==" " or $USDArray[$i]==PHP_EOL) {
        $k=$k+1;
        //echo ($k."<br>");
            if ($k>1) {
                unset($USDArray[$i]);
            }
    } else {$k=0;}
}
var_dump($USDArray);

I am using for loop to get rid of extra spaces in the string. I deliberately do not want to use Regular Expressions.

Unfortunately the code cannot get rid off ALL EXTRA spaces. The result of var_dump:

array(34) { [0]=> string(1) "U" [1]=> string(1) "S" [2]=> string(1) "D" [3]=> string(1) " " [4]=> string(1) " " [5]=> string(1) " " [37]=> string(1) "1" [38]=> string(1) "." [39]=> string(1) "5" [40]=> string(1) "0" [41]=> string(1) "9" [42]=> string(1) "4" [43]=> string(1) " " [44]=> string(1) " " [45]=> string(1) " " [77]=> string(1) "1" [78]=> string(1) "." [79]=> string(1) "4" [80]=> string(1) "6" [81]=> string(1) "4" [82]=> string(1) "1" [83]=> string(1) " " [84]=> string(1) " " [85]=> string(1) " " [117]=> string(1) "1" [118]=> string(1) "." [119]=> string(1) "5" [120]=> string(1) "5" [121]=> string(1) "1" [122]=> string(1) "7" [123]=> string(1) " " [124]=> string(1) " " [125]=> string(1) " " [157]=> string(1) ";" }

It leaves 3 spaces between words (array elements 4 &, 5), though I want to keep anly one space.

Could anybody suggest what I am doing wrong. Grasias! :)

UPDATE:

What I noticed when looked at the result of var_dump using Chrome view-source:

array(34) { [0]=> string(1) "U" [1]=> string(1) "S" [2]=> string(1) "D" [3]=> string(1) " " [4]=> string(1) "" [5]=> string(1) " " .... ....

See items [4] and [5]. I have feeling that these are NOT spaces... Any suggestion?

UPDATE 2

I used ORD function to convert and print every single symbol ASCII code: for ($i=0; $i<$TotalSymbols; $i++) { print_r(ord($USDArray[$i])."<br>"); }

and discovered that the some of white spaces were only pretending white spaces :) Part of output:

85 - U 83 - S 68 - D 13 <- Carriage Return 10 <- Line Feed 32 - Ordinary Space 32 - Ordinary Space ... ... ... Symbols with codes 10 & 13 are guilty!

Upvotes: 1

Views: 75

Answers (4)

RST
RST

Reputation: 3925

I think your trouble is with the unset, it makes the array skip elements. You could try something like this:

$k=0;
$dummy_array = $USDArray;

foreach ($dummy_array as $key => $value) {
    //var_dump($USDArray[$i]);
    if ( " " == $value || $value == PHP_EOL || '' == $value) {
        $k=$k+1;
        //echo ($k."<br>");
            if ($k>1) {
                unset($USDArray[$key]);
            }
    } else {$k=0;}
}

Upvotes: 1

Andreas
Andreas

Reputation: 23958

The only resonable thing is to use regex to do it.

$count = null;
$returnValue = preg_replace('/(\\s{2,})/', ' ', 'USD  11    99', -1, $count);


result: 'USD 11 99'

Upvotes: 0

Ravi Hirani
Ravi Hirani

Reputation: 6539

Use below code:-

$USDArray = ['0' => 'A', '1' => 'B', '2' => ' ', '3' => ' ','4' => ' ', '5' => 'C','6' => ' ', '7' => ' '];
$USDArray = array_map('trim', $USDArray); // remove white space from array elements
for($i=0;$i<=count($USDArray);$i++){
    if (empty($USDArray[$i]) && empty($USDArray[$i+1])){        
          unset($USDArray[$i+1]);   
    }
}
echo '<pre>'; print_r($USDArray); 

OR

$USDArray = ['0' => 'A', '1' => 'B', '2' => ' ', '3' => ' ','4' => ' ', '5' => 'C','6' => ' ', '7' => ' '];
$USDArray = array_map('trim', $USDArray); // remove white space from array elements
foreach($USDArray as $k=>$v){
    if (empty($USDArray[$k]) && empty($USDArray[$k+1])){        
           unset($USDArray[$k+1]);   
    }
}
echo '<pre>'; print_r($USDArray); 

output:-

Array
(
    [0] => A
    [1] => B
    [2] => 
    [5] => C
    [6] => 
)

Note:- use array_values() it returns all the values from the array and indexes the array numerically.

echo '<pre>'; print_r(array_values($USDArray)); 

output:-

Array
(
    [0] => A
    [1] => B
    [2] => 
    [3] => C
    [4] => 
)

Upvotes: 0

Andreas
Andreas

Reputation: 23958

if ($USDArray[$i]==" " && $USDArray[$i+1]==" "){ 
    $k = $i;
    While($USDArray[$k]==" ") {
        Unset($USDArray[$k]);
        $k++;
    }
    $i = $k+1;
}

Untested and written on my phone. So there could be some mistakes.

Upvotes: 0

Related Questions