I'm scraping some items from a website but there's a problem.
There is a website where the scraped price has a whitespace between the valuta symbol and the first number. But because of that whitespace, my price won't be added to the cart total when the items are added to the shoppingcart...
I've already tried preg_replace()
So I know I should use str_replace to remove the whitespaces but it just won't change.. Am I doing something wrong? My code:
$decprs = json_encode($articles[$i]->prijs);
//$decodeprijs = "€ 9.99"
$decodeprijs = json_decode($decprs);
$nospace = (string) str_replace(' ', '', $decodeprijs);
$nocomma = str_replace(',', '.', $nospace);
$nohyphen = str_replace('-', '0', $nocomma);
$officialprijs = $nohyphen;
dd($officialprijs);
dd -> "€ 9.99"
CodePudding user response:
This did the trick:
$nospace = preg_replace("/[\pZ\pC] /u", "", $nohyphen);
CodePudding user response:
Another approach can be to extract the decimal number / currency symbol:
$decodeprijs = "€ 9.99";
//decimal number matches
preg_match_all('!\d \.*\d*!', $decodeprijs, $decimal_matches);
preg_match_all('/€/', $decodeprijs, $euro_sign_matches);
print_r ($decimal_matches);
/*
Array
(
[0] => Array
(
[0] => 9.99
)
) */
print_r ($euro_sign_matches);
/*
Array
(
[0] => Array
(
[0] => €
)
)
*/