1
<?php
$string = file_get_contents("csv.csv");
$array = explode(",", $string);
$q = strtolower($_GET[q]);
foreach ($array as $value) {
    $result = explode(":", $value);
if (strpos($q, $result[0]) !== false) {
    $output = $result[1];
}
}

echo $output;
?>

Here is the content of the file csv.csv which I am turning into a string.

hello: how are you doing,
hi: what are you,
df:df

If $_GET[q] (and $q) is hello, the $output is how are you doing. However, if it is hi, I do not get the output what are you or if I do df I do not get df.

Any reason why this is occuring? Thank you in advance for your kind help.

6
  • $_GET[q] should be $_GET['q']. Commented Aug 1, 2014 at 1:40
  • Make sure to turn on error_reporting and display_errors, always in development. PHP will be complaining about an undefined constant q in $_GET[q]. error_reporting(E_ALL); ini_set('display_errors', 1); always when developing code. Commented Aug 1, 2014 at 1:42
  • @Darren Good point from a purely syntaxical point of view, but really I've used $_GET[q] a tireless number of times, and it's no different from $_GET['q']. Also, I'm sure it has nothing to do with the problem that occurs... Commented Aug 1, 2014 at 1:44
  • @MichaelBerkowski Error reporting is on. Commented Aug 1, 2014 at 1:44
  • @testo but it is different. I causes PHP to use a behavior which is intended to save you from yourself, whereby it converts constants it doesn't know about into strings. It is a bad thing to rely on. If you have error reporting turned up to E_ALL (not suppressing notices) you will most certainly see notices that look like Use of undefined constant q - assumed 'q' Commented Aug 1, 2014 at 1:46

2 Answers 2

1

You are exploding by commas, but the truth is you have each value separated by comma plus line break.

After exploding, your array is ["hello","\nhi...","\ndf:..."], that's why there's no match for the strpos comparison.

try

$array = explode(",\n", $string);

Edit: as @Michael Berkowski said, you could also trim the parameter

if (strpos($q, trim($result[0])) !== false)

The order of the parameters depends on what kind of partial match do you want to offer. With your current parameter order, the parameter "hi" would match "hi", "h" and "i" but not "high".

If you flip them as Michael suggest, the parameter "hi" would match "hi" and "high" but not "h" or "i".

3
  • To clarify what went wrong, the args to strpos() are $haystack, $needle, so you are searching $q whose value is 'hi' for the literal string 'hi\n' and that does not match. It would match if you flipped the args strpos($result[0], $q) instead, but trimming the linebreak either in explode or later with trim() is the solution. Commented Aug 1, 2014 at 1:44
  • Manually parsing a CSV is ugly and resulted in this exact problem, you should use str_getcsv as it will automatically take care of this issue Commented Aug 1, 2014 at 1:49
  • I use fgetcsv, the only caveat is to check the setting auto_detect_line_endings Commented Aug 1, 2014 at 1:54
1

Instead of manually parsing a CSV use str_getcsv

Using str_getcsv and replacing $_GET[q] with $_GET['q'] fixes the issue

    $csv = file_get_contents('csv.csv');
    $array = str_getcsv($csv);
    var_dump($array);
    $q = $_GET['q'];
    foreach ($array as $value) {
        $result = explode(":", $value);
        if (strpos($q, $result[0]) !== false) {
            $output = $result[1];
        }
    }

    echo $output;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.