Luke Appel
Luke Appel

Reputation: 371

CSV find and replace with quotation marks with PHP

Trying to figure out what I am dong wrong here but I am trying to search row 6 and replace the phrase "Plenty of stock available!" (with the quotation marks as it is in a CSV) with the number "10".

It works no issues when then with the 'lowstock' text, but not the "Plenty of stock available!". I have added the \ before it as to have it ignore the quotation mark (and view it as text).

 //changes stock status
    $row = fgetcsv($file);
    if (strtolower($row[6]) == '\"Plenty of stock available!\"' || strtolower($row[6]) == 'lowstock') {
    $row[6] = '10';
    } else if (strtolower($row[6]) == 'nostock') {
    $row[6] = '2';

Upvotes: 0

Views: 85

Answers (1)

Tigger
Tigger

Reputation: 9120

I would try the following:

$row = fgetcsv($file);
// Don't modify the original data (unless you need to) and 
// remove duplicate calls to strtolower()
$r6 = strtolower($row[6]);
// Only test against lower case letters, " should not be needed
if ($r6 == 'plenty of stock available!' || $r6 == 'lowstock') {
    // perform correction / change
    $row[6] = '10';
} else if ($r6 == 'nostock') {
    // perform correction / change
    $row[6] = '2';
}

Upvotes: 1

Related Questions