3

Correct usage is: /,,([,]+)?|^,|,$|\b,\b|\s,/

$comma[0] = '/,,([,]+)?/';  
            $comma[1] = '/^,/';     
            $comma[2] = '/,$/';
            $comma[3] = '/\b,\b/';  
            $comma[4] = '/\s,/';    

$analyst = preg_match($comma, $_POST['analyst']) 
    ? mysql_real_escape_string($_POST['analyst']) : NULL;

I am trying to detect commas from the users input, each regex is defined properly, but I do not understand why it isn't passing them into the if statement.

Edit:

if I change this:

$analyst = preg_match('test', $_POST['analyst']) 
    ? mysql_real_escape_string($_POST['analyst']) : NULL;

then it works, this makes no sense.

9
  • Try reading the documentation: php.net/preg_match -- of course it fails, it doesn't accept an array. Nothing odd or unexpected at all. Commented May 3, 2011 at 17:08
  • @Erik: People get confused, because preg_replace does. Commented May 3, 2011 at 17:08
  • No, Orbling, people just don't bother to look at basic documentation :( Commented May 3, 2011 at 17:10
  • 1
    A helpful answer would have been try using the pipe. /,,([,]+)?|^,|,$|\b,\b|\s,|,/ Commented May 3, 2011 at 17:17
  • Just out of idle curiosity, what could you possibly be trying to match against that str_pos( $_POST['analyst'], ',' ) wouldn't suffice? Commented May 3, 2011 at 17:20

2 Answers 2

4

preg_match() doesn't accept arrays as arguments, only strings.

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

http://php.net/manual/en/function.preg-match.php

Sign up to request clarification or add additional context in comments.

2 Comments

Well there you go. I just assumed it did. That is freaking weird. Thanks
Although it didn't solve my problem, it allowed me to learn how to use the pipe.
1

preg_match accept first parameter as string and you are passing an array.

2 Comments

Is there a technical reason for this? It seems like an oversight to me.
@Ryan: Take a look at posted link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.