<?php
include "DB_class.php";
// ini_set('error_reporting', E_ALL);
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
header('Cache-Control: max-age=360000, must-revalidate');
header('Expires: Mon, 26 Jul 2040 05:00:00 GMT');
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=utf-8');
DB::$user = 'user';
DB::$password = 'password';
DB::$encoding = 'utf8';
DB::$host = '127.0.0.1';
DB::$dbName = 'database';
date_default_timezone_set('UTC');
$search = strip_tags($_GET['q']);
$startTime = microtime(true);
$array = array();
$startPos = 0;
$takeRecords = 15;
if (isset($_GET['start']) && is_numeric($_GET['start'])) {
$startPos = $takeRecords * $_GET['start'];
}
$query = <<<SQL
SELECT
id,
id as catid,
title,
catdesc as body,
'c' as mtype,
'' as tab
FROM category as outab
WHERE upper(title) LIKE upper('%$search%') OR upper(catdesc) LIKE upper('%$search%')
UNION ALL
SELECT
ct.id as id ,
ct.catid as catid,
c.title as title,
tab1 as body,
'a' as mtype ,
'1' as tab
FROM content as ct
inner join category as c
ON ct.catid = c.id
WHERE upper(tab1) LIKE upper('%$search%')
UNION ALL
SELECT
ct.id as id ,
ct.catid as catid,
c.title as title,
tab2 as body,
'a' as mtype ,
'2' as tab
FROM content as ct
inner join category as c
ON ct.catid = c.id
WHERE upper(tab2) LIKE upper('%$search%')
UNION ALL
SELECT
ct.id as id ,
ct.catid as catid,
c.title as title,
tab3 as body,
'a' as mtype ,
'3' as tab
FROM content as ct
inner join category as c
ON ct.catid = c.id
WHERE upper(tab3) LIKE upper('%$search%')
LIMIT $startPos, $takeRecords
SQL;
$res = DB::query($query);
foreach ($res as $r) {
$id = $r['id'];
$catid = $r['catid'];
$title = $r['title'];
$mtype = $r['mtype'];
$body = cleanUpForMe($search, $r['body']);
$tab = $r['tab'];
$result[] = array('id' => $id, 'catid' => $catid, 'title' => $title, 'body'=>$body, 'mtype' => $mtype, 'tab' =>$tab );
}
$endSql = microtime(true);
if(!isset($result))
$result = [];
$res_array = array('items' => $result, 'total' => count($result), 'exe_time'=> $endSql - $startTime);
echo json_encode($res_array, JSON_UNESCAPED_UNICODE);
function cleanUpForMe($query, $text){
$text = strip_tags($text);
$text = str_replace("\r\n",' ', $text);
$text = str_replace(" ",' ', $text);
preg_match_all("/[A-ZА-Я].*?[.!?](?=\s|$)/ui", $text, $matches);
$outText = '';
if(count($matches[0]) > 0){
foreach ($matches[0] as $m){
preg_match_all("/.* $query .*/ui", $m, $mm);
if(count($mm[0]) > 0){
$outText = $mm[0][0];
break;
}
}
}
else
$outText = $text;
return $text = str_replace($query, "<b>{$query}</b>", $outText);
}
Немного не разбираюсь в PHP, но возник вопрос stripos упорно игнорирует кирилические строки. По идее я хотел вычленить первое предложение в котором встречается поисковая фраза. Поэтому все происходит в 4 этапа,
1. собственно сам запрос
2. очистка исходных данных от "мусора"
3. регулярка вычленяющая предложения
4. регулярка отдающая первое предложение в цикле.
По идее 4 шаг можно заменить на stripos, но не работает.
Так же по идее 4 шаг можно совместить с 3 им, но тоже не вышло, не хватает компетенции.
Если не трудно помогите.