John Davidson

php - str_replace() doesn't remove whitespace

0 comments
Message:


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"


My Answer:
The `str_replace()` function in PHP is used to replace all occurrences of a substring within a string with another substring. It takes three parameters: the substring to search for, the substring to replace it with, and the string to search within.

Here is an example of how to use the `str_replace()` function:

php
$string = "Hello, world!";
$newString = str_replace("world", "PHP", $string);

echo $newString; // Output: Hello, PHP!


In this example, the `str_replace()` function replaces the substring "world" with "PHP" in the original string "Hello, world!".

Rate this post

4 of 5 based on 4637 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us