1

I have a PHP script that loops through each row of a CSV file and organizes each line into an array:

$counter = 0;
    $file = file($ReturnFile);
    foreach($file as $k){
        if(preg_match('/"/', $k)==1){
            $csv[] = explode(',', $k);
            $counter++;
         }
     }

    ...

    while($x<$counter){
        $line=$csv[$x];

This works; my question is about how to find a substring within each line. This:

    foreach($line as $value){
        if($value==$name_search){
            // action

works if the value of $line is exactly equal to the value of $name_search ($name_search is a person's last name). However, this doesn't work if there is a space or additional characters in the value of $line (for example: $line equal to "Wilson (ID: 345)" or "Wilson " won't match a $name_search value of "Wilson".

So far I've tried:

if(strpos($value, $res_name_search) !== false){

if(substr($value, 0, strrpos($value, ' '))==$res_name_search){

if(substr(strval($value), 0, strrpos(strval($value), ' '))==$res_name_search){

without success ... Do I have a syntax error and/or is there a better way to accomplish this?

1
  • I think the issue should be related to the Upper Case or Lower Case. Try lower case both strings. strpos() could work. Commented Apr 11, 2016 at 14:49

7 Answers 7

1

I think you have inverted the parameters. The following should work:

if (strpos($res_name_search, $value) !== false)

A minor note: use stripos for case-insensitive search.

Sign up to request clarification or add additional context in comments.

2 Comments

No, the first parameter is the haystack ($line) and the second is the needle ($name_search), so @faalbane's order of the function arguments is correct
tested; same issue
0

Try to use strpos like this: if (strpos($res_name_search, $value))

1 Comment

tested; same issue
0
  1. use php TRIM function
  2. Convert to either of the lowercase or uppercase before compairing
  3. use var_dump to check the data type
  4. instead of using var_dump type cast $value and $name_search to STRING
  5. also check ===
  6. Remove spaces (if required)
  7. Use regular expression to remove (, ), :, -, ; etc...
  8. and of course apply function strpos

You can apply above mentioned points in your logic (Order of points may be different)

Comments

0

Try this:

$str = 'This is my test: wilson ';
$search = "wilson";

if(strpos(strtolower($str), strtolower($search)) !== false){
    echo 'found it';
}

2 Comments

tested; same issue
then should be something else in your code. If you test this code here. phptester.net will work. Try it!
0

You can also try this:

if (preg_match('/'.strtolower($res_name_search).'/', strtolower($value)))

1 Comment

tested; same issue
0

This is the sort of situations for which the built-in PHP function stristr exists. This function is the case-insensitive equivalent of the strstr function which, according to the docs:

Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack.

Using this function, achieving such a task becomes as easy as:

foreach($line as $value){
    if( stristr($value, $name_search) !== FALSE){
        // substring was found in search string, perform your action

You can read more about it in the official documentation

I hope this helps.

2 Comments

tested; same issue
Ok, the confusion was coming from the way you used $value and $name_search. I've inverted the order of the two variables in my answer. That should fix your problem.
0

I advice you to use fgetcsv function, this will return you an array of your columns for each iteration as follow :

 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
 // ...
 }

If in your CSV file, the column NAME is in position 2 for example and you want to know if token exist, just use

if (strpos('Wilson', $data[2] !== FALSE) {
    // do your job
}

if you want to deal with case-insentivie, use stripos function

Comments

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.