开发者

PHP Regex Number in Round Brackets

开发者 https://www.devze.com 2023-04-11 06:04 出处:网络
I simply want to get numbers out of a string, where the numbers are contained in round brackets. i have the following code:

I simply want to get numbers out of a string, where the numbers are contained in round brackets.

i have the following code:

   $type_raw = $row['Type'];
   $length_pattern = '^\(\d+\)$';

   preg_mat开发者_StackOverflowch($type_raw,$length_pattern,$match,PREG_OFFSET_CAPTURE);
   $length = $match[0];

PHP gives me the following error:

"Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in -filename- -line number->"

Is there something wrong with my regex pattern or what else could it be?

Ok i just changed

  $length_pattern = '^\(\d+\)$';

to

  $length_pattern = '/^\(\d+\)$/';

and the same error pops up?


From PHP.net

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

So yours will be:

$length_pattern = '/^\(\d+\)$/'; // with a delimiter "/"

preg_match($length_pattern,$type_raw,$matches,PREG_OFFSET_CAPTURE);


You need to provide a delimiter for your regex:

$length_pattern = '/^\(\d+\)$/';


The method signature is:

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

So you need to reverse your order, and provide a delimiter for your regex, e.g. /^\(\d+\)$/

0

精彩评论

暂无评论...
验证码 换一张
取 消