Reputation: 152
I have a problem with spaces within array $a2
. I would like to replace " "
with ""
.
I tried
$a2 = str_replace(" ", "", $a2);
and even :
function str_replace_json($search, $replace, $subject) {
return json_decode(str_replace($search, $replace, json_encode($subject)), true);
}
$a2 = str_replace_json(" ", "", $a2);
But it trims spaces only before and after number (not inside).
My array:
$a2 = array( $rowData[3][1],
$rowData[3][2],
$rowData[3][3],
$rowData[3][4],
$rowData[3][5],
$rowData[3][6],
$rowData[3][7]
);
var_dump of my array:
array(7) { [0]=> string(54) " 155 808.00 "
[1]=> string(54) " 131 256.00 "
[2]=> string(54) " 106 008.00 "
[3]=> string(53) " 60 600.00 "
[4]=> string(53) " 41 520.00 "
[5]=> string(52) " 5 880.00 "
[6]=> string(52) " 6 744.00 "
}
What might be a reason for that?
EDIT When I declere my array this way:
$a2 = array(
" 155 808.00 ",
" 131 256.00 ",
" 106 008.00 ",
" 60 600.00 ",
" 41 520.00 ",
" 5 880.00 ",
" 6 744.00 "
);
I can easily trim all the spaces (including those inside numbers).
This is how i create $rowData
include_once ("includes/php/simple_html_dom.php");
$html = file_get_html('https://gaz.tge.pl/pl/rdn/gas/index/index/');
$table = $html->find("table[@class=t-02]",0);
$rowData = array();
foreach($table->find('tr') as $row) {
$data = array();
foreach($row->find('td') as $cell) {
$data[] = $cell->plaintext;
}
$rowData[] = $data;
}
array_unshift($rowData[0], "RDNpg");
var_dump
of $rowData
:
array(7) { [0]=> array(8) { [0]=> string(5) "RDNpg" [1]=> string(68) " Pn. 13/02 " [2]=> string(68) " Wt. 14/02 " [3]=> string(69) " Śr. 15/02 " [4]=> string(68) " Cz. 16/02 " [5]=> string(68) " Pt. 17/02 " [6]=> string(68) " So. 18/02 " [7]=> string(67) " N. 19/02 " } [1]=> array(1) { [0]=> string(40) " TGEgasDA " } [2]=> array(8) { [0]=> string(8) "PLN/MWh " [1]=> string(44) " 92.56 " [2]=> string(44) " 91.36 " [3]=> string(44) " 89.51 " [4]=> string(44) " 87.62 " [5]=> string(44) " 88.01 " [6]=> string(44) " 84.63 " [7]=> string(44) " 84.90 " } [3]=> array(8) { [0]=> string(4) "MWh " [1]=> string(54) " 155 808.00 " [2]=> string(54) " 131 256.00 " [3]=> string(54) " 106 008.00 " [4]=> string(53) " 60 600.00 " [5]=> string(53) " 41 520.00 " [6]=> string(52) " 5 880.00 " [7]=> string(52) " 6 744.00 " } [4]=> array(1) { [0]=> string(40) " TGEsgtDA " } [5]=> array(8) { [0]=> string(8) "PLN/MWh " [1]=> string(40) " - " [2]=> string(40) " - " [3]=> string(40) " - " [4]=> string(40) " - " [5]=> string(40) " - " [6]=> string(40) " - " [7]=> string(40) " - " } [6]=> array(8) { [0]=> string(4) "MWh " [1]=> string(40) " - " [2]=> string(40) " - " [3]=> string(40) " - " [4]=> string(40) " - " [5]=> string(40) " - " [6]=> string(40) " - " [7]=> string(40) " - " } } array(7) { [0]=> string(54) " 155 808.00 " [1]=> string(54) " 131 256.00 " [2]=> string(54) " 106 008.00 " [3]=> string(53) " 60 600.00 " [4]=> string(53) " 41 520.00 " [5]=> string(52) " 5 880.00 " [6]=> string(52) " 6 744.00 " }
And json_encode($rowData);
[["RDNpg"," \t Pn. \t 13\/02 \t "," \t Wt. \t 14\/02 \t "," \t \u015ar. \t 15\/02 \t "," \t Cz. \t 16\/02 \t "," \t Pt. \t 17\/02 \t "," \t So. \t 18\/02 \t "," \t N. \t 19\/02 \t "],[" \t TGEgasDA \t "],["PLN\/MWh "," \t 92.56 \t "," \t 91.36 \t "," \t 89.51 \t "," \t 87.62 \t "," \t 88.01 \t "," \t 84.63 \t "," \t 84.90 \t "],["MWh "," \t 155 808.00 \t "," \t 131 256.00 \t "," \t 106 008.00 \t "," \t 60 600.00 \t "," \t 41 520.00 \t "," \t 5 880.00 \t "," \t 6 744.00 \t "],[" \t TGEsgtDA \t "],["PLN\/MWh "," \t - \t "," \t - \t "," \t - \t "," \t - \t "," \t - \t "," \t - \t "," \t - \t "],["MWh "," \t - \t "," \t - \t "," \t - \t "," \t - \t "," \t - \t "," \t - \t "," \t - \t "]]
EDIT 2 I used bin2hex()
to see what is realy inside $a2
array. I got:
20200920202020202020202020202020202020313535266e6273703b3830382e30302020200920202020202020202020202020202020
20200920202020202020202020202020202020313331266e6273703b3235362e30302020200920202020202020202020202020202020
20200920202020202020202020202020202020313036266e6273703b3030382e30302020200920202020202020202020202020202020
202009202020202020202020202020202020203630266e6273703b3630302e30302020200920202020202020202020202020202020
202009202020202020202020202020202020203431266e6273703b3532302e30302020200920202020202020202020202020202020
2020092020202020202020202020202020202035266e6273703b3838302e30302020200920202020202020202020202020202020
2020092020202020202020202020202020202036266e6273703b3734342e30302020200920202020202020202020202020202020
Am I right that mysterious space is no-break space
and that is why trim function with normal space doesn't work?
Upvotes: 2
Views: 288
Reputation: 434
Use array_map
. The code will look like this:
$originalArray = json_decode($jsonText, true);
$data = array_map(function($value){
return str_replace(" ", '', $value);
}, $originalArray);
var_dump($data);
Later Edit: Looks like the requirements of the problem got changed and the same the input data. This changes everything as well. You can see here http://php.net/array_map how it works, it's simpler and cleaner.
So, having the array with this data (let's take only the first key-value)
// this is the actual data from the array
$a = "20200920202020202020202020202020202020313535266e6273703b3830382e30302020200920202020202020202020202020202020";
// make it readable
$b = hex2bin($a);
// see what is inside
var_dump($b);
var_dump will return something like:
string(54) " 155 808.00 "
So, you have
which is 6 characters written and displayed only one.
What solution I see in this case would be to use trim
function to remove the spaces from the beginning and the end of the string, and then to use preg_replace
to remove all non digit characters and dots.
$b = trim($b);
$b = preg_replace("/([^0-9\.]+)/", '', $b);
The result will be then:
string(9) "155808.00"
So, the end result will look like this:
$data = array_map(function($value){
$value = trim($value);
return preg_replace("/([^0-9\.]+)/", '', $value);
}, $originalArray);
Upvotes: 1
Reputation: 15547
Try this out:
<?php
$rowData = array(
array(
"RDNpg",
" Pn. 13/02 ",
" Wt. 14/02 ",
" Śr. 15/02 ",
" Cz. 16/02 ",
" Pt. 17/02 ",
" So. 18/02 ",
" N. 19/02 ",
),
array(" TGEgasDA "),
array(
"PLN/MWh ",
" 92.56 ",
" 91.36 ",
" 89.51 ",
" 87.62 ",
" 88.01 ",
" 84.63 ",
" 84.90 "),
array(
"MWh ",
" 155 808.00 ",
" 131 256.00 ",
" 106 008.00 ",
" 60 600.00 ",
" 41 520.00 ",
" 5 880.00 ",
" 6 744.00 "
),
array(
" TGEsgtDA ",
),
array(
"PLN/MWh ",
" - ",
" - ",
" - ",
" - ",
" - ",
" - ",
" - "
),
array(
"MWh ",
" - ",
" - ",
" - ",
" - ",
" - ",
" - ",
" - "
)
);
$a2 = array($rowData[3][1], $rowData[3][2], $rowData[3][3], $rowData[3][4], $rowData[3][5], $rowData[3][6], $rowData[3][7]);
function trimmed($item)
{
return str_replace(" ", "", $item);
}
$a2trim = array_map('trimmed', $a2);
var_dump($a2trim);
Here is the output:
array(7) {
[0]=>
string(9) "155808.00"
[1]=>
string(9) "131256.00"
[2]=>
string(9) "106008.00"
[3]=>
string(8) "60600.00"
[4]=>
string(8) "41520.00"
[5]=>
string(7) "5880.00"
[6]=>
string(7) "6744.00"
}
That will apply the trimmed function to the elements in the array.
Upvotes: 2
Reputation: 2109
you need to iterate
foreach($a2 as $index => $a2str){
$a2[$index] = str_replace(" ", "", $a2str);
}
Upvotes: 0
Reputation: 114
Personally when removing whitespaces I use Preg_replace rather than str_replace
Try this:
preg_replace('/(\s)/gm', '', $a2);
Upvotes: -1