P
P
posters2019-08-26 15:01:46
PHP
posters, 2019-08-26 15:01:46

How to implement non-strict search in php?

Is there a way to organize a primitive non-strict search without using complex libraries, binding to the input language and databases?
The input is something like this:
$myarray = ['lit', 'leaves', 'Moscow', 'leaves', 'flip',];
$findme = 'sheet';
and at the output we get something like:
Array
(
[0] => sheet
[1] => leaves
[2] => sheet va
[3] => flip the sheet )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2019-08-26
@posters

$search_text = 'лист';

array_filter($array, function($el) use ($search_text) {
        return ( strpos($el['text'], $search_text) !== false );
    });

https://github.com/loilo/Fuse
spoiler
<?php
require_once 'vendor/autoload.php';

$fuse = new \Fuse\Fuse([
  [
    "title" => "Old Man's War",
    "author" => "John Scalzi"
  ],
  [
    "title" => "The Lock Artist",
    "author" => "Steve Hamilton"
  ],
  [
    "title" => "HTML5",
    "author" => "Remy Sharp"
  ],
  [
    "title" => "Right Ho Jeeves",
    "author" => "P.D Woodhouse"
  ],
], [
  "keys" => [ "title", "author" ],
]);

$fuse->search('hamil');

/*
Array
(
  [0] => Array
    (
      [title] => The Lock Artist
      [author] => Steve Hamilton
    )
  [1] => Array
    (
      [title] => HTML5
      [author] => Remy Sharp
    )
)
*/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question