ПРОГРАММ: 654 | СТАТЕЙ: 125 | КОДОВ: 3434   

    главная        новости        программы        библиотека        коды        рейтинг       форум   

Афоризм
Не всякий лось перекусит рельсу.
 
Поиск


 
Рассылка
| Soft-Ежедневник - обзор программ прямо к Вам на e-mail
 
 
Полезное!
 
7 лучших
  Алмазный инструмент
  Зоомагазин в г. Можайск
  Films&Games
  Amarish - питомник британских кошек
  Студия дизайна интерьера АПЕЛЬСИН. Дизайн-проект дома, квартиры, коттеджа, офиса, ресторана. Киев.
  
  
 
  Библиотека > web - разработка > PHPдобавить статью
Безопасный и удобный поиск

Главное, с чем сталкиваешься при написании скрипта для поиска - то, что все кажется простым, но объем кода быстро нарастает...
 

infocity.kiev.ua
10-05-2004
Версия для печати
печать

Главное, с чем сталкиваешься при написании скрипта для поиска - то, что все кажется простым, но объем кода быстро нарастает.

Обработка строки


Первым делом надо порезать ручками строку.

$search = substr($search, 0, 64);

64 символов пользователю будет достаточно для поиска. Теперь каленым железом выжжем все "ненормальные" символы.

$search = preg_replace("/[^(w)|(x7F-xFF)|(s)]/", " ", $search);

По идее, нельзя давать пользователю возможности искать по слишком коротким словам - кроме всего прочего, это сильно загружает сервер. Итак, разрешим искать только по словам, которые длиннее двух букв (если ограничение больше, надо заменить "{1,2}" на "{1, кол-во символов}").

$good = trim(preg_replace("/s([^s]{1,2})s/", " ", ereg_replace("[ ]%2B", "  "," $search ")));

А после замены плохих слов - надо сжать двойные пробелы (они были сделаны специально для корректного поиска коротких слов).

$good = ereg_eplace("[ ]%2B", " ", $good);

Логика


Допустим, мы хотим предоставить пользователю возможность выбирать логику поиска - искать все слова или только одно из нескольких. Если вы хотите сделать как в Яндексе [2] [1] - два амперсанта означают "И" (слово1&&слово2&&слово3) или как-то еще, то я не советчик. Шаманство со строками на небольшом сайте imho не оправдывает затраченного времени. Поэтому форму для поиска рисуем так:


А в поисковом скрипте лишний раз проверяем, что пользователь ввел:

if ($logic!="AND" && $logic!="OR")
  $logic = "OR";

Как будет использоваться логика — ниже.

Релевантность


Наверное, в том же Яндексе [2] [1] все видели ссылочку "сортировать по релевантности". Это оно и есть. Сортировка результатов по количеству совпадений слов.

Отчасти, кстати, такая сортировка снимает проблему обработки логики поиска. Но с БД MySQL делать такую сортировку очень сложно. Надо сперва выбрать, где есть все слова, потом записи, где разные слова (исключив предыдущие). Если у вас постраничный вывод - то вообще дело труба!

Статистика поиска


Неплохо будет сразу информировать пользователя, сколько он нашел строк таблицы. Для этого делается дополнительный запрос в базу:

$query = "SELECT id FROM table WHERE field LIKE "%". str_replace(" ", "%" OR field LIKE "%", $good). "%"";

Для статистики по отдельным словам можно сделать следующее:

$word = explode(" ", $search);
while (list($k, $v) = each($word)) {
  if (strlen($v)>2)
    $stat[]="$v:". mysql_num_rows(mysql_query("SELECT id FROM table WHERE field LIKE "%$v%""));
  else
    $stat[]="$v: <font color=#cc0000>короткое</font>";
  };
$word_stats = "Статистика слов: ". implode("", $stat). "<br>";
unset($stat);

Постраничный вывод результатов


Ну, когда у нас есть макет для поиска и количество строк результата поиска, сделать постраничный поиск - пара пустяков. Проверяем переменную $page (не меньше 0, не больше $results_amount/$rows_in_page).

В запрос, который подсчитывает количество строк (смотри выше), пишем нужные нам поля и поля для сортировки. А потом дописываем

if ($page==0)
  $request .= "LIMIT $rows_in_page";
else
  $request .= "LIMIT ". $page*$rows_in_page. ",". $rows_in_page;

(синтаксис: LIMIT <кол-во строк> либо LIMIT <кол-во строк отступа>, <кол-во строк>)

В результате выполнения подобного запроса мы получим именно те самые строки, которые надо выводить на странице.

Для навигации можно либо рисовать ссылки на следующую и предыдущую страницы, либо, что сложнее, делать панель навигации на несколько страниц.

if ($page>0)
  print ("<a href=search.php?search=". rawurlencode($good). "&page=". ($page-1). ">предыдущая страница</a>");

if ($page<$results_amount/$rows_in_page)
  print ("<a href=search.php?search=". rawurlencode($good). "&page=". ($page%2B1). ">следующая страница</a>");

Подсветка


Чтобы подсвечивать светом или жирным шрифтом искомые слова в тексте, надо сделать всего лишь следующее:


$highlight = "(". str_replace(" ", "|", $good). ")";

Пробелы (а они у нас между словами стоят поодиночке, и нигде двойной пробел не встречается, к тому же с концов строки мы их тоже вырезали) достаточно заменить на вертикальную черту - разделитель вариантов в регулярных выражениях. "Плохие" слова мы не подсвечиваем, потому что в базе их не ищем :).

В коде, который выводит текст пишем:


$row["text"] = ereg_replace($highlight, "<font color=#cc0000>1</font>", $row["text"]);

После написания выпуска я кинулся, было, писать и себе "подсветку". Не тут-то было! У меня в тексте встречаются теги HTML, поэтому пришлось много подумать... Получилась вот такая вещь (строка со словами для подсветки есть):

$text = eregi_replace(">([^<]*)$words", ">1<font color=#cc0000>2</font>3<", $text);

Приходится смотреть, нет в теге ли это слово. Однако тут встает проблема ресурсоемкости такой замены (мой K6-266 над текстом в 5 килобайт думал целых семь секунд). Печально.

Итог


Применяя такие приемы, можно, во-первых, ограничить свободу действий пользователя и не дать ему а) узнать программную структуру сайта б) вызвать перегрузку сервера (например, отправив мегабайт текста, состоящего из слов длиной в три буквы (фраза получилась двусмысленная, но переписывать не буду :), чтобы скрипт 250 тысяч раз лазил в базу) в) увидеть сообщение об ошибке в результате попадания в строку спецсимволов языка запросов. Во-вторых, некоторое удобство для пользователя - постраничный вывод и подсветка.



Комментарии
Оставить комментарий
страница < | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | >

DennisSwaminnap | 30-07-22 22:01
Furthermore, normal ranges vary considerably, depending on the method used, so it's the general purpose of this guideline is to supply essential to interpret take a look at ends in the context of the clinicians with practical steering for the administration of appropriate reference range. The half-life of sildenafil and vardenafil is about four hours, suggesting that the normal window of efficacy is 6-8 h following drug ingestion, although responses following this time period are nicely recognised. Incorporating information from the captive inhabitants and survival and population estimates from the wild will refine that erectile dysfunction treatment nyc <a href=http://www.charlesedisonfund.org/med-research/order-online-levitra-jelly/>best order for levitra jelly</a>.
This is an ovarian teratoma arising from argentaffin cells of intestinal epithelium within the teratoma. For right sided procedures, a 5mm trocar is commonly positioned in the higher midline close to the xyphoid course of to accommodate a traumatic locking grasper forceps which might grasp the diaphragm and hold the 39 liver up exposing the higher pole of the kidney, adrenal and suprarenal vena cava. Granulocyte-macrophage colony stimu- and tumor cells produce fundamental fibroblast progress issue erectile dysfunction market <a href=http://www.charlesedisonfund.org/med-research/order-levitra-online-no-rx/>purchase 20mg levitra with visa</a>. Compromised blood supply leads to infarct of the subchondral bone ben


BenitoAccerce | 30-07-22 11:23
Applicants for first or second-class should present this information yearly; candidates for third-class must present the information with each required examination. Your employer to take away duties that could is required to try to implement prove bodily tough or reasonable adjustments and seventy two. This report was written with the coordination and help of an Internal Response Team on the European Centre for Disease Prevention and Control allergy medicine good for allergies to cats <a href=http://www.charlesedisonfund.org/med-research/purchase-desloratadine-online/>desloratadine 5 mg mastercard</a>.
Metformin is, at greatest, no more effective, and, based mostly on a big multi-center trial, less efficient than clomiphene alone. If the patient is in an intermittent atrial fibrillation and whether it is rapid and inflicting signs, medicines may be used to try to convert the rhythm to regular sinus rhythm or a cardioversion (electrical shock to the heart, utilized beneath sedation) could also be used. How can there be complete occlusion of the internal carotid arteries and this patient not have been dead at the time initial evaluation treatment centers for alcoholism <a href=http://www.charlesedisonfund.org/med-research/order-cheap-selegiline-online-no-rx/>order selegiline 5mg line</a>. Many of the research with surgical or procedural interventions reported intermediate outcomes corresponding to technical success, hospital size of stay, or estimates of blood loss related to


MarikKneeplE | 28-07-22 08:58
Medical orders and the discharge ommendations advise individualization в—Џ Must be receptive to learning plan of care must be applicable, based mostly on therapy goals, physiologic pa- в—Џ Tend to be problem-focused quite achievable, and agreeable to the patient rameters, and drugs utilization; these than subject-oriented and family. If tachycardia develops on phenoxybenzamine remedy the physician will add another medicine to regulate the guts rate. Yet, typically, they stay in place by way of all ages and constitute the main a part of nevi observed in aged patients medicine lake montana <a href=https://www.smgriffith.org/course/order-diamox-online-no-rx/>purchase discount diamox on line</a>.
You may be acquainted with a statistical hypothesis test, such as a chi-sq. test or a T-check. Pain with intercourse Occurs from pelvic and vaginal congestion, uterine enlargement, and changing hormone ranges. Bone-anchored sound conduction techniques have been utilized in Europe since 1977 and had been approved in the United States in 1996 as a treatment for conductive and combined hearing losses antimicrobial yoga flooring <a href=https://www.smgriffith.org/course/purchase-cheap-zymycin/>order zymycin cheap online</a>. One can assume that a pilot with the above signs is taking some type of treatment. Blue Cross and Blue Shield reserves the proper to require submission of a duplicate of the Assignment of Benefit PayпїЅ ment. Previously, essentially the most used and most reliable of t


GivessNoceele | 26-07-22 23:37
H o w e v e r, o n e m u s t e x c l u d e the possibility of an encephalocele or a highly vascular lesion corresponding to an angiofibroma; thus imaging earlier than biopsy is prudent. Another sort of biofeedback remedy is fnger-pulse biofeedback, which is achieved by attaching an electrode to the fnger. The optimal monitoring of protein-energy nutritional status requires the collective evaluation of a number of parameters (ie, assessment of visceral protein, muscle mass or somatic pro tein, body composition) antibiotic x-206 <a href=http://www.padrugdiscovery.org/biomedical/buy-online-trimethoprim-cheap-no-rx/index.html>generic trimethoprim 960mg</a>.
Safe however rapid transport from the excessive-altitude setting to a decrease altitude setting Patient Presentation Inclusion Criteria 1. Studies show that endure repeated botulinum toxin A injection as a result of time to most effect for sublingual nifedipine elevated adhesion of the muscular layer. Because many medication are excreted in human milk, caution ought to be exercised when extracts are administered to a nursing woman arthritis knee weight loss <a href=http://www.padrugdiscovery.org/biomedical/purchase-cheap-feldene-online/index.html>purchase generic feldene on line</a>. Further, it can be expected that as the Provincial Surgical Oncology Program is developed, more sufferers will be accessing most cancers centres for a multidisciplinary oncology consultation previous to their surgery. Miconazole (Micatin) or Clo


Domenikeracricky | 25-07-22 05:22
Lesions most commonly occur in diaper and intertriginous areas but in addition elsewhere on the body. Humanure composters can stand beneath the stars at night gazing on the heavens, and know that, when nature calls, their excretions won't foul the planet. DirectorsпїЅ Leadership Academy is available by way of the Center for Early Childhood Professional Development ( symptoms questionnaire <a href=http://www.jamesonministries.org/institute/order-asacol/>discount asacol 400 mg with amex</a>.
However survival is 35% decrease (in stool screening take a look at prior to now year or a sigmoidoscopy absolute terms) in adults 85+ than in ages 65-eighty four for or colonoscopy in the past 5 or 10 years, respectively. Extravasation Potential of Chemotherapeutic Agents A trained and experienced staff will greatly lower process-associated extravasation risk components. Isolation of micro organism from bone or adjacent constructions confirms the prognosis, but is constructive in only about one-third of cases acne extractions <a href=http://www.jamesonministries.org/institute/purchase-online-isotretinoinum-no-rx/>buy cheap isotretinoinum 40 mg line</a>. The lateral ventricle which causes neuronal destruction, metabolic problems opposite to the hematoma has turn into enlarged consequently typically trigger only minor neuropathologic modifications. Diabetic supplies together with lancets, alcohol pads and testing strips are in profit. In all events, 70% of participants did not total treatm


WenzelOsciday | 20-07-22 13:04
The consequence of oncogenic transformation is that tumour cells turn into impartial of these external development signaling factors in any regular tissue microenvironment. Erectile dysfunction is considered one of many symptoms of sexual issues including untimely ejaculation, increased latency time related to age, psycho-sexual relationship problems, and lack of libido. Doxycycline is appro lated price to repletefiuid standing over 48 hours, priate remedy for Chlamydia an infection and but not any faster antibiotics bladder infection <a href=https://maicc.org/lectures/order-ordipha-online/>ordipha 100mg free shipping</a>.
The sensitivity of all microscopy-based mostly strategies is dependent upon the skill of the laboratory technician. More invasive testing must be reserved for advanced sufferers, sufferers who have failed preliminary therapies. The node enlargement, and oropharyngeal manifesta- skin is rough, hyperpigmented, and a number of tions coronary artery quiz <a href=https://maicc.org/lectures/purchase-cheap-propranolol-online/>propranolol 80 mg lowest price</a>. Evaluation of the integrity, composition, and differentiation state of the genome is critical for the development of patient-particular pluripotent stem cells for therapeutic use (see Chapter thirteen). Moderate hyperhomocysteinaemia and immune activation in patients with rheumatoid arthritis. Despite these conclusions, no-until agriculture could not be practiced with out the arrival of herbicides and the


JesperCrunseben | 18-07-22 20:45
Unfortunately, when the affected person is discharged, What elements result in poor ache the depth or continuity of ache care is disrupted. Risk factors for high-order a number of pregnancy and a number of delivery after managed ovarian hyperstimulation: outcomes of four,062 intrauterine insemina- tion cycles. Neonates with a single mutation in factor V Leiden aren't at increased threat for thrombosis steroids and antibiotics for sinus infection <a href=http://www.scotsindallas.org/adviser/order-azifast-online/>generic 500mg azifast with amex</a>.
More than half of infants with • explosive, watery, foul-smelling stool on rectal omphalocele have related anomalies and preoperative examination. Transplantation of organs similar to the center could be an option, but it depends on a plentiful one cell type (hepatocytes) and has some in-constructed capacity source of transplant organs, and plenty of are briefly supply. It is strongly beneficial that children be checked annually submit chemotherapy to detect a secondary malignancy bacterial throat infection <a href=http://www.scotsindallas.org/adviser/buy-cheap-nitrofurantoin-online/>order nitrofurantoin 100 mg mastercard</a>. Shankaran S: Neonatal encephalopathy: remedy with hypothermia, the risk of pneumonia is 5% to twenty%. The dealing with and cutting of uncooked meat are prone to permit the release of meat aerosolized allergens into the environment, as already demonstrated in crab-exposed employees. Efficacy and security o


OsmundHeeltluro | 15-07-22 23:12
Comvax shouldn't be given before 6 weeks of age as a result of Haemophilus influenzae sort b component, and Twinrix just isn't yet accredited in the United States to be used in persons less than 18 years old. We addressed those points by using the totally different views of the staff to reach at a trustworthy interpretation and asking an professional Project Advisory Group to oversee and critique the evolving interpretation. A current case-management research in Greece (Bohlke et al, 1999) involved 820 girls with histologically confirmed breast most cancers who were in contrast with 1548 control ladies virus attack <a href=http://www.worldcell.com/development/order-goutichine-online-no-rx/>purchase goutichine toronto</a>.
Perhaps somewhat than trying to discover higher it becomes Sarafem (fluoxetine hydrochloride, Warner Chilcott). There was also proof that feedback could not only enhance a psychological well being outcome for particular person service users but additionally might accomplish that through an influence on healthcare skilled behaviour. However, there was a major lower within the variety of cardiac nuclei in feminine pups (2) allergy treatment 3 phases <a href=http://www.worldcell.com/development/buy-promethazine-online-no-rx/>best promethazine 25 mg</a>. However, sensitivity for identifying women levels (<1 ng/mL) have been related to poor responses who will reply poorly varies extensively (10% eighty%) (22). Do not use frst names until invited to do quickly e


LukarLoask | 11-07-22 15:18
In chronic arthropathy there is cartilage destruction and narrowing of the joint house. The thyrotoxic section is most typical at 2 to пїЅ Since you have been pregnant, have you been hit, slapped, 3 months postpartum, whereas the hypothyroid part often kicked, or in any other case bodily harm by someonefi. Zinc: Patients with extensive illness in their small gut are in danger for zinc defciency breast cancer north face <a href=http://www.yhwh.com/experience/purchase-ginette-35-online-no-rx/>proven 2mg ginette-35</a>.
This handbook provides a short introduction to the themes of sanitation and hygiene. Additionally, infants born to ladies enrolled in these research proceed to be seen for a number of months after delivery and infrequently endure further tests. The following phrases have the meanings set forth in the corresponding Sections of this Agreement: Term Section пїЅAbandoned Collaboration PatentsпїЅ 9 acne hairline <a href=http://www.yhwh.com/experience/purchase-flexresan-no-rx/>purchase flexresan 30mg with mastercard</a>. The therapeutic value of acupuncture within the treatment of assorted pain conditions, together with osteoarthritis; migraine; and low-again, neck, and knee ache has rising proof in the form of systematic critiques and meta-analyses. Vulval pores and skin issues Topical antibiotics are of no use for treatment of Vulval skin circumstances also can present with vulval vaginal infection. Dysfunction of the tendon of tibialis posterior is proven by oedem


Juliohak | 01-07-22 09:23
It is unknown whether or not the distinction displays, at least in part, different regulatory traditions. Selectable marker genes additionally can be included that, for example, present resistance to antibiotics or toxins, complement auxotrophic deficiencies, or supply important nutrients not in the culture media. Am J Kidney Dis 13:390-395, 1989 der pain syndrome and delicate-tissue abnormalities in patients 360 milligrams of cholesterol in eggs <a href=http://www.avion.com/programs/purchase-atorlip-20-no-rx/>order 20mg atorlip-20 with mastercard</a>.
They should be assured of not just care, but in addition understanding, respect and excellence. Non bilious aspirate Aspirate containing undigested method fi Use breast milk wherever out there as it favors extra fast gastric emptying than formulation. Aberrant deve- lopment may produce a cell which has twice the conventional chromosomal complement (4n) stages for hiv infection <a href=http://www.avion.com/programs/order-cheap-valacyclovir-online/>purchase 500mg valacyclovir with visa</a>. Depending upon the particular sample of correct and incorrect responses, candidates will obtain totally different numbers of items and due to this fact use varying amounts of time. With the introduction of new medicine, numerous studies have questioned their privi- Tis technique was frst described in the treatment of pain after a unilateral rib fracture. It Complete (basic) mole by cytogenetic studies has been weighs 400-600 gm or about one-

Имя:
E-mail:
Сообщение:


Число=
укажите сумму чисел на картинке

   очистить

популярные статьи по теменовые статьи
Создание видео из фотографий в программе "ФотоШОУ"
Разное / Обработка фото и видео
Больница для компьютера
железо / Система
Чем так популярны социальные сети?
Разное / разное
Синхронизация данных с сотового телефона на ПК
железо / Мобильные устройства
Куплю хостинг
web - разработка / Хостинг

Опрос
На что Вы обращаете внимание в первую очередь при покупке товара в интернет-магазине?  
 
Комментарии и оценку пользователей
Наличие нескольких фотографий
Подробное и качественное описание
Условия доставки
Возможность покупки в кредит
 
Самое свежее  
 ПРОГРАММЫ

ICQ 8.0.5990
ICQ 8.0.5990

Mozilla Firefox 18.0.2
Mozilla Firefox 18.0.2

ArtMoney 7.40.4
ArtMoney 7.40.4

 НОВОСТИ

Хакерская атака на Google из Китая
Хакерская атака на Google из Китая

Мы победили! Олимпиада 2014 пройдет в Сочи
Мы победили! Олимпиада 2014 пройдет в Сочи

 БИБЛИОТЕКА

Создание видео из фотографий в программе "ФотоШОУ"
Создание видео из фотографий в программе "ФотоШОУ"

Больница для компьютера

 
Рекомендую!



Copyright © 2004-2024 MostInfo.net  | связь с админом
При использовании материалов сайта ссылка на MostInfo обязательна.