Forum Moderators: coopster

Message Too Old, No Replies

regexp matching in a comma-delimited list?

I *kinda* got it but I have a slight problem.

         

HughMungus

6:38 am on Apr 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's the *right* way to find word matches to words in a comma-delimited list.

For example, if I have the following comma-delimited list of categories in a mysql db field:

gameboy, nintendo, playstation

and I do a search, I know I can use regexp to do something like:

select * from categories where regexp 'gameboy,'

Notice that I have the comma in there to match the whole word and the comma without matching part of a word (to prevent unwanted matches such as "play" to "playstation" or "game" to "gameboy").

The problem I'm running into is words that match that match the end of each word next to the comma (in this example, "boy" and "station").

What's the right way to match a word *exactly* using regexp *without* also matching *part* of a word. Don't need the specifics, but some guidance would be great.

TIA

dcrombie

9:30 am on Apr 20, 2005 (gmt 0)



[php.net...] (Example 2)

;)

killroy

12:53 pm on Apr 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use lookahead and lookbehind:

(?<,¦^)\s*([^,]*?)\s*(?=,¦$)

This means match a string that is preceeded by the beginning of the subject OR a comma, then any amount of whitespace, then a string that does not include a comma (lazy, so we don't catch traling whitespace) then any amount of whitespace and then followed by either a comma or the end of hte subject.

SN

Spudstr

2:29 pm on Apr 20, 2005 (gmt 0)

10+ Year Member



if its comma.. you could always use explode() and in_array()

$Myarray = implode(",",$myList);
if(in_array("searString",$Myarray))
{
// found
} else {
// not found.
}