I am trying to str_replace a string in my foreach loop, but it won't work on one of the strings, but it works perfectly on another string, both in the same loop.
The code:
<?php
function round_up($value, $places){
$mult = pow(10, abs($places));
return $places < 0 ?
ceil($value / $mult) * $mult :
ceil($value * $mult) / $mult;
}
$ch = curl_init();
$limit = $_GET["limit"];
$page = $_GET["page"];
curl_setopt($ch, CURLOPT_URL, 'https://api.com/v1/products?limit=' . $limit . '&page=' . $page);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Api-Key: 1234567890';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$resArr = array();
$resArr = json_decode($response);
echo 'ID,Type,Varenummer,Navn,Udgivet,"Er fremhævet (udvalgt)?","Synlighed i katalog (shoppens frontend)","Kort beskrivelse",Beskrivelse,"Den dato hvor tilbudsprisen starter","Den dato hvor tilbudsprisen slutter",Momsstatus,Momsklasse,"På lager?",Lager,"Lavt lagerantal","Restordrer tilladt?","Sælges enkeltvist?","Vægt (kg)","Længde (cm)","Bredde (cm)","Højde (cm)","Tillad kundeanmeldelser?","Bemærkning til køb",Tilbudspris,Normalpris,Kategorier,Tags,Forsendelsesklasse,Billeder,Downloadbegrænsning,"Download udløbsdage",Forældre,"Grupperede varer",Mersalg,Krydssalg,"Ekstern URL (webadresse)","Knap tekst",Position,"Swatches Attributes"<br>';
foreach($resArr->results as $resultsData){
$productName = $resultsData->originalName;
$productDesc = str_replace('"', "", $resultsData->description);
$productPlatform = $resultsData->platform;
$productPrice = $resultsData->price * 7.44;
$productPrice = round_up($productPrice, 0);
$productActivationDetails = str_replace('"', '', $resultsData->activationDetails);
echo ',"simple, virtual",,"' . $productName . '",1,0,"visible","' . $productDesc . '","' . $productDesc . '",,,"taxable",,1,,,0,0,,,,,0,"' . $productActivationDetails . '",' . $productPrice . ',' . $productPrice . ',,"' . $productPlatform . '",,,,,,,,,,,0,<br>';
}
An example of the results that i get is:
ID,Type,Varenummer,Navn,Udgivet,"Er fremhævet (udvalgt)?","Synlighed i katalog (shoppens frontend)","Kort beskrivelse",Beskrivelse,"Den dato hvor tilbudsprisen starter","Den dato hvor tilbudsprisen slutter",Momsstatus,Momsklasse,"På lager?",Lager,"Lavt lagerantal","Restordrer tilladt?","Sælges enkeltvist?","Vægt (kg)","Længde (cm)","Bredde (cm)","Højde (cm)","Tillad kundeanmeldelser?","Bemærkning til køb",Tilbudspris,Normalpris,Kategorier,Tags,Forsendelsesklasse,Billeder,Downloadbegrænsning,"Download udløbsdage",Forældre,"Grupperede varer",Mersalg,Krydssalg,"Ekstern URL (webadresse)","Knap tekst",Position,"Swatches Attributes"
,"simple, virtual",,"Borderlands 2",1,0,"visible","Five years after the events of Borderlands, a valuable mineral called "Eridium" starts flourishing through Pandora's crust. Handsome Jack, the leader of the Hyperion Corporation, secures this new resource and uses it to rule over Pandora with an iron fist. Meanwhile, rumors of an even larger Vault hidden on Pandora spread across the galaxy, drawing a new group of Vault Hunters to the planet in search of it.","Five years after the events of Borderlands, a valuable mineral called "Eridium" starts flourishing through Pandora's crust. Handsome Jack, the leader of the Hyperion Corporation, secures this new resource and uses it to rule over Pandora with an iron fist. Meanwhile, rumors of an even larger Vault hidden on Pandora spread across the galaxy, drawing a new group of Vault Hunters to the planet in search of it.",,,"taxable",,1,,,0,0,,,,,0,"Go to: http://store.steampowered.com/ and download STEAM client Click Install Steam (from the upper right corner) Install and start application, login with your Account name and Password (create one if you don't have). Please follow these instructions to activate a new retail purchase on Steam: Launch Steam and log into your Steam account. Click the Games Menu. Choose Activate a Product on Steam... Follow the onscreen instructions to complete the process. After successful code verification go to the MY GAMES tab and start downloading.",17,17,,"Steam",,,,,,,,,,,0,
My problem here is that the str_replace doesn't work on the $productDesc
variable, but it works perfectly on the $productActivationDetails
variable. Both is in the same loop, and i dont know if im going crazy, or there is something really simple that i have missed.
The problem is at the 8th and 9th column:
"Five years after the events of Borderlands, a valuable mineral called "Eridium" starts flourishing through Pandora's crust. Handsome Jack, the leader of the Hyperion Corporation, secures this new resource and uses it to rule over Pandora with an iron fist. Meanwhile, rumors of an even larger Vault hidden on Pandora spread across the galaxy, drawing a new group of Vault Hunters to the planet in search of it."
For some reason it doesn't want to remove the double quotation marks around the word Eridium, and also in some of the other results later on.
Is there something that i need to do differently when im using str_replace in a foreach loop?
I appreciate all help, thanks in advance.
All credit goes to @CBroe
The solution was to str_replace
the HTML entity, instead of normal quotation marks.
My solution was:
$productDesc = str_replace('"', "", $resultsData->description);
$productDesc = str_replace('"', "", $resultsData->description);
$productDesc = str_replace('"', "", $resultsData->description);
$productDesc = str_replace('"', "", $resultsData->description);
I replaced all the entities that CBroe mentioned, just to future proof it, so no such errors happen, or are less likely to happen.