PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Regex for Advanced search feature
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Regex for Advanced search feature

Réponse
 
LinkBack Outils de la discussion
Vieux 19/10/2007, 17h48   #1
Kevin Murphy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Regex for Advanced search feature

I'm trying to create an advanced search feature for my site, and I
have it mostly working the way I want. I take whatever search term
($searchkey) that the user submits, explodes if off any spaces
between words, and then use that to search for each word separately.

$keys = explode(" ",$searchkey);

So the search phrase of:

Dog Cat Horse

would output :

Array
(
[0] => Dog
[1] => Cat
[2] => Horse
)

However, I would like to add the ability to have phrases in there
indicated by " marks. So, if the search phrase was this:

"Dog Cat" Horse

It would output:

Array
(
[0] => Dog Cat
[1] => Horse
)

My concept to solve this is right before I explode the keys as above,
to replace any spaces within " marks with a garbage string of
characters (say XXXXXXXXXX) using a regular expression, and then
later replace it back with a space. However, I suck at regular
expressions and can't figure it out.

Anyone have a way for me to accomplish this with a regular expression
or with another method?

--
Kevin Murphy
Webmaster: Information and Marketing Services
Western Nevada College
www.wnc.edu
775-445-3326

P.S. Please note that my e-mail and website address have changed from
wncc.edu to wnc.edu.



  Réponse avec citation
Vieux 19/10/2007, 19h55   #2
Al
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regex for Advanced search feature

Go here and get charprobe http://www.dextronet.com/charprobe.php It's a super
utility to have.

Look up the hex code for a space, "/x20" as I recall.

Then pick a non-printable code that you like e.g., /x83 or a printable one that
users cannot enter.

Then simply preg_replace("%/x20+%", "/83", $string); //one or more spaces

If you want to preserve the number of spaces don't use "+".


Kevin Murphy wrote:
> I'm trying to create an advanced search feature for my site, and I have
> it mostly working the way I want. I take whatever search term
> ($searchkey) that the user submits, explodes if off any spaces between
> words, and then use that to search for each word separately.
>
> $keys = explode(" ",$searchkey);
>
> So the search phrase of:
>
> Dog Cat Horse
>
> would output :
>
> Array
> (
> [0] => Dog
> [1] => Cat
> [2] => Horse
> )
>
> However, I would like to add the ability to have phrases in there
> indicated by " marks. So, if the search phrase was this:
>
> "Dog Cat" Horse
>
> It would output:
>
> Array
> (
> [0] => Dog Cat
> [1] => Horse
> )
>
> My concept to solve this is right before I explode the keys as above, to
> replace any spaces within " marks with a garbage string of characters
> (say XXXXXXXXXX) using a regular expression, and then later replace it
> back with a space. However, I suck at regular expressions and can't
> figure it out.
>
> Anyone have a way for me to accomplish this with a regular expression or
> with another method?
>

  Réponse avec citation
Vieux 19/10/2007, 20h23   #3
Robert Cummings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Regex for Advanced search feature

On Fri, 2007-10-19 at 09:48 -0700, Kevin Murphy wrote:
> I'm trying to create an advanced search feature for my site, and I
> have it mostly working the way I want. I take whatever search term
> ($searchkey) that the user submits, explodes if off any spaces
> between words, and then use that to search for each word separately.
>
> $keys = explode(" ",$searchkey);
>
> So the search phrase of:
>
> Dog Cat Horse
>
> would output :
>
> Array
> (
> [0] => Dog
> [1] => Cat
> [2] => Horse
> )
>
> However, I would like to add the ability to have phrases in there
> indicated by " marks. So, if the search phrase was this:
>
> "Dog Cat" Horse
>
> It would output:
>
> Array
> (
> [0] => Dog Cat
> [1] => Horse
> )
>
> My concept to solve this is right before I explode the keys as above,
> to replace any spaces within " marks with a garbage string of
> characters (say XXXXXXXXXX) using a regular expression, and then
> later replace it back with a space. However, I suck at regular
> expressions and can't figure it out.
>
> Anyone have a way for me to accomplish this with a regular expression
> or with another method?


<?php

$foo = '"dog cat" horse mouse "tiger fish"';

if( preg_match_all( '/[[:space:]]+(("[^"]*")|([^[:space:]]+))/', '
'.$foo, $bits ) )
{
print_r( $bits );
}

?>

Cheers,
Rob.
--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h25.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,95230 seconds with 11 queries