Re: Good Regex for general text search

From: Date: Wed, 03 Oct 2007 02:28:51 +0000
Subject: Re: Good Regex for general text search
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-262664@lists.php.net to get a copy of this message
Amos Vryhof wrote:
Ok, My example was pretty bad. I was just looking for an easier/more optimal way to look for something along the lines of "word1*word2*word3*word4" rather than "word1 word2 word3 word4" as it currently does. Actually, I think I have an idea for a way to do what I want without a regular expression.... I'll have to see how fast it is though before actually using it.
Not guaranteed to work at all but if you really want to use a regex you could do something like this: $words = array('word1', 'word2', 'word3'); $search = '%'; foreach ($words as $word) { $search .= '\\\b'.preg_quote($word, '%') . '\\\b.*?'; } $search .= '%is'; preg_match($search, $string, $found_matches); So you should end up with something like: preg_match('%\bword1\b.*?\bword2\b.*?\bword3\b%is', $string, $found_matches); See http://www.php.net/preg_quote and http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php and http://www.php.net/manual/en/reference.pcre.pattern.syntax.php -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.general (#262664) next »