filterBy case sensitive

hello , using the custom filter function , example :

I’m filtering a user list containing a name from a form . my problem is that if a name is capitalized only searches for names with capital letters and let me out the names lowercase …

$q = get("query");
$users->filterBy("firstName", "*=", $q );

if q$ = “daniel” then search results only have daniel or that contains daniel but no “Daniel”

if $q = “Daniel” then search results only have Daniel but no “daniel”

is there a way that filter function noy being case sensitive?

thanks

Yes, the filters are case-sensitive by default.

You can however copy the *= filter and place a modified copy of it in your config.php (for example with the name i*=. The filter will then be available with that name.

how can i make the filter not case sensitive? changing the operator === for ==?

I think you could replace strpos with stripos: http://php.net/stripos

thanks, i just already figured out. thanks

Hi Plantalabs,
could you share your code in the config file and the way to implement that in a filterBy scenario? I really can’t figure it out, even with all the answers here…

thanks,
Matthias

Hi. I am at work right now. When at home I post complete answer.

This should do it:

collection::$filters['i*='] = function($collection, $field, $value, $split = false) {
  foreach($collection->data as $key => $item) {
    if($split) {
      $values = str::split((string)collection::extractValue($item, $field), $split);
      foreach($values as $val) {
        if(stripos($val, $value) === false) {
          unset($collection->$key);
          break;
        }
      }
    } else if(stripos(collection::extractValue($item, $field), $value) === false) {
      unset($collection->$key);
    }
  }
  return $collection;
};

hi, sorry i am late.

you have to create a file (filters.php) on plugins folder with the content
that textnixe write

then…

you use like:

<?php if(str::length($nombre_p) > 0): ?>
<?php $usuarios = $usuarios->filterBy('firstName', 'i*=', $nombre_filtro);
?>
<?php endif; ?>

Thanks to all. (Completely forgot to answer this).
It all works now.

1 Like