E
E
Evgen Nova2015-10-15 18:19:12
PHP
Evgen Nova, 2015-10-15 18:19:12

Problem with ajax + php live search script, misunderstanding with reading from mysql, how to solve?

There is a script for live search, but it works somehow clumsily, it searches in English letters, but doesn’t want to in Russian .... I tried many different settings, but nothing helps, maybe someone has met with such questions and knows the solution where to pick?

It seems that the problem is in a piece of php code

$search_string = preg_replace("/[^A-Za-z0-9А-Яа-я]+$/", "", $_POST['query']);


does not want to recognize Russian characters in the database ....
after entering 2 characters, he writes that there is nothing.
7bb49a26f8624a9c9ba7df190f36575c.jpg

If you write in English, then everything works...
4b0fbeadc4f7464ebdbd4e858c759b45.jpg

This is already the third version of the "live search", which we managed to install and connect.... but all of them are not quite what we need, the search is carried out by characters in order, i. if (for example, the word in the database: "diaper for children 2-3 years old 50 pcs.") Write in the search query "diaper 50" will not find anything, since there are no such phrases. Probably some addition is needed in the form of an "autocomplete" script or something else ..... maybe someone knows a similar script?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mikhail Osher, 2015-10-15
@miraage

So have you tried it?
// EDIT
Looks like the end-of-line modifier is superfluous.

$strings = [
    'foo',
    'фУbar',
    'бар',
    '53252',
    '-x-x-x-x-'
];

foreach ($strings as $string) {
    var_dump(preg_replace('/[^A-Za-z0-9А-Яа-я]+/u', '', $string));
}

string(3) "foo"
string(7) "фУbar"
string(6) "бар"
string(5) "53252"
string(4) "xxxx"

S
Stalker_RED, 2015-10-15
@Stalker_RED

And if you remove preg_replace does the search work? Maybe an encoding problem? Look with a debugger or output to the log what kind of request to the database is being formed.

E
Evgen Nova, 2015-10-15
@evgen_nova

I met a similar problem on some site, so they solved it in this way

preg_replace("#[^А-ЯA-Z]#iu", " ", $_POST['query']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question