Hello,

I have build an html interface with a text area. I want a script that would let me search for text pasted there based on a database. how do I go about it

Dani AI

Generated

Good question, . ’s pointer toward pattern matching and @diafol’s caution about a dictionary-sized lookup are the right starting points. Choice of technique should be driven by two things: the matching behavior needed (exact word, stemmed form, fuzzy/approximate) and the size of the keyword set.

For small lists, normalize the textarea (casefold, remove or normalize punctuation, handle Unicode normalization), tokenize into words, and compare tokens against a single in-memory keyword set fetched once and cached. For medium-size vocabularies that need many substring matches, a multi-pattern algorithm like is efficient because it scans in linear time. For large vocabularies, ranking needs, or fuzzy matching, use a DB full-text index or a search engine instead; see Full-Text Search in MySQL.

Practical cautions: do not run one query per keyword; fetch and cache the keyword set, or build a trie/Aho-Corasick automaton once. Use PCRE (the modern PHP regex engine) rather than obsolete POSIX functions (see the PHP preg_match docs at https://www.php.net/manual/en/function.preg-match.php) and handle UTF-8 with multibyte-aware routines (see https://www.php.net/manual/en/book.mbstring.php). Always profile with realistic input and watch memory when loading large dictionaries.

Recommended Answers

All 2 Replies

use regular expressions
ereg_math or preg_match
if you are not familiar with regular expresion, use simple strpos('textarea content','searched word');

Member Avatar for Member #120589

You need to download the php manual. Just a word of warning. If you're running a dictionary's worth of words from a db - this'll bite you in the posterior!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.