Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stefverniers's avatar

str_replace not working..

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...

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"
0 likes
4 replies
click's avatar

What is the purpose of json_encode() and immediately doing json_decode() on the same value?

The space could also be something else than the normal space, there are several characters in HTML that could look like a space.

What is the difference between these two?

var_dump($decodeprijs);
var_dump($officialprijs); 

Try

$nospace = preg_replace("/\s+/", "", $decodeprijs);

It removes (almost) all types of spaces like tabs, spaces, new lines

stefverniers's avatar

@click The difference between those two is just the comma has been changed by a point because of the converting methods I used... I've already tried the preg_replace() method and that doesn't do much ether...

click's avatar
click
Best Answer
Level 35

@stefverniers then it is not a regular space but another character that looks like a space. Without the actual string I can't see which one works but try one of the following

Try appending this one; it removes the non breaking space: https://en.wikipedia.org/wiki/Non-breaking_space

$nospace = (string) str_replace(' ', '', $decodeprijs);
$nospace = preg_replace('/\xc2\xa0/', '', $nospace);

Or try one of these:

$nospace = preg_replace("/\s+/u", "", $decodeprijs);
// or; 
$nospace = preg_replace("/[\pZ\pC]+/u", "", $decodeprijs);

An alternative approach is to simply remove everything except the characters you DO want to keep. So for example you could make a regex to remove everything except digits, comma, dot, minus and a euro and dollar sign.

$input = 'For Sale: € 999  ,  9   9   ';
$output = preg_replace('/[^\d.,\-$€]/', '', $input)
$output = str_replace(',', '.', $output);
$output = str_replace('-', '0', $output);

// output = €999.99

But beware of this last approach that it could give strange results if you your input contains a value like: Buy 1, Get 2 for 4.50. The result would be 124.50.

stefverniers's avatar

// I used this one but removed the whitespace between the space between the double quotes

$nospace = preg_replace("/[\pZ\pC]+/u", " ", $decodeprijs);

Thank you so much!!

Please or to participate in this conversation.