Word delimiters? preg_match PHP

potworny used Ask the Experts™
Hi there...

I am going to kill myself with a spoon in a minute...

I'd like to match string that begin with the word "ball":

I tried the following:
/\bball/im = no luck
/\bball\b/im = no luck
/ball/ = positive
I'd like this to match as follows
Ball is red. = match
Balls are red = match
RedBall is red = no match

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®




And what about ball in the following sentences? :)

This is a ball that is red. = match
I have a lot of balling to do. = match

Open in new window

Terry WoodsIT Guru
Most Valuable Expert 2011

is correct - can you post your code, and maybe I can find a problem with it?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.


that's odd.

$regex = '/^[bB]all.*/';

is saying to match 'ball' as the first chars of the string, and any other non new-line character after (.*).


The database contains:


$query = "SELECT blackword_pattern FROM tbl_blackwords";
$countRows = $db->query($query);
while( $countRow = $db->fetch_array($countRows)){
      if( preg_match($countRow['blackword_pattern'], $string) ) return true; # found something
return false; #found nothing

Open in new window

IT Guru
Most Valuable Expert 2011
Can you print out the contents of $countRow['blackword_pattern'] just to be sure it's what you expect. For example, sometimes result array key names need to be upper case so the value you're wanting might be $countRow['BLACKWORD_PATTERN']. Or something else might be happening which still makes it worth checking.

Secondly, can you output the contents of $string, and check that is what you expect too.


I made a stupid mistake. Yes.
The database contains "/\bball/im"
There should be "/\\bball/im"
(double backslash)

Thank you very much for your help guys. You saved one life :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial