Jump to content

Search in string doesn't work


Sjorsman

Recommended Posts

I'm trying to filter out the rows of a uploaded csv that have the word "lokaal" (classroom) in the 3th colom, but this doesn't work.

 

What does work:

  • When I search for just one letter (for example the o) $findneedle is true, so it works
  • Putting $haystack="lokaal 5"; in the coding also works (but that is just usable for testpurposes)
  • echo $haystack does result in a correct value, so the csv is read correctly

 

<?
$handle = fopen($_FILES['file']['tmp_name'], "r");
$data = fgetcsv($handle, 1000, ";"); //Remove if CSV file does not have column headings
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) 
 {

    $haystack=$data[2];
    $needle='lokaal';
    $findneedle=strpos($needle, $haystack);

    if($findneedle !== false) 
    {
    //rest of the coding
    }
?>

 

 

 

The CSV looks like this

 

2013;Class 1;Zelfstudie;31-12-2013 0:00:00;1-1-1900 9:00:00;1-1-1900 12:00:00;

2013;Class 2;lokaal 5;27-12-2013 0:00:00;1-1-1900 15:00:00;1-1-1900 17:00:00;

 

Am I overlooking something? I hope someone can help me.

Link to comment
Share on other sites

Thanks for the reply. I made a mistake when I created the topic. My original coding is in Dutch and when I rewrote the variable names to needle and haystack I switched them. This is how it should have been. Sorry for this stupid mistake. 

 

<?
$handle = fopen($_FILES['file']['tmp_name'], "r");
$data = fgetcsv($handle, 1000, ";"); //Remove if CSV file does not have column headings
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) 
 {

    $haystack=$data[2];
    $needle='lokaal';
    $findneedle=strpos($haystack, $needle);

    if($findneedle !== false) 
    {
    //rest of the coding
    }
?>
Link to comment
Share on other sites

Nope, still doesn't work. I've been experimenting further and it looks like it's a charset problem, because when I re-save the csv-file as an UTF-file (in Notepad) and upload that one, everything works fine. If I do a mb_detect_encoding on the $haystack it echo's ASCII. Since ASCII is a subset of UTF-8, to me it doesn't make sense why it does work when I re-save the file as UTF8.

 

Now I'm trying to do the re-save automatically by php. I've playing around with mb_convert_encoding, iconv and utf8_encode, but with no succes so far. If anyone has good suggestions for it, please let me know.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.