Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex Not Working

Status
Not open for further replies.

altendew

Programmer
Mar 29, 2005
154
US
Here is what I am trying to accomplish, I am making a search feature through PHP and MySQL, I am using MYSQL REGEXP feature to search.

I need to make the regex search for fullwords aligned. Meaning...

Lets say there is a entry named "This is alot of fun"

These would match it:

This
This is
This is alot
is alot
of fun

These wouldnt match it

This alot (missing "is")
is fun (missing "alot of")

Here is my php code.

Code:
	unset($newSearch);
	$searchStr = preg_quote(rtrim(trim($_POST['search'])), '/');
	$var = preg_split("/\s/", $searchStr);
	$num = count($var);
	$key=1;
	foreach($var as $word)
	{
		if($key=='1')
		{
			$newSearch .= '^(.*\s*)?'.$word;
		}
		else
		{
			$newSearch .= $word;
		}
		if($key!=$num)
		{
			$newSearch .= '[\s]+';
		}
		$key++;
	}
	$newSearch .= '(\s*.*)?$';

I left out the MySQL code because its not important.

Now there is a entry called "Just for (re) FUN (ding)"

In the search box I typed "just for"

$newSearch was "^(.*\s*)?just[\s]+for(\s*.*)?$"

I dont understand why it didnt match?? Please help!!
 
I'm a perl developer rather than php developer but the regex should be similar:

Code:
/\b(\w+(\s+\w+)*)\b/

\b is for word boundary
\w is word character (a-z0-9 etc)
brackets are for grouping and capturing.

Hope this helps.


Trojan.

 
why not use like clause?
...where like '%This is a lot%'

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top