1
votes

I am trying to use preg_match to extract some data from some html. I want to get the number from the following line of an Ajax call

new Ajax.Request('/selectdata/new/get_ids?id=687420'

So, I have used the following PHP

preg_match('new Ajax.Request\(\'/selectdata/new/get_ids\?id=(.+?)\'', $buffer, $matches)

I get the message "preg_match(): Delimiter must not be alphanumeric or backslash"

I have tried lots of variations, but no success.

1

1 Answers

6
votes

The first character in the first argument has to be the delimiter. Since it's "n", preg_match thinks you're trying to have an 'n'-delimited regexp.

Add pipes, acquire success:

preg_match('|new Ajax.Request\\(\'/selectdata/new/get_ids\\?id=(.+?)\'|', $buffer, $matches)

Testing:

$ php --interactive
php > $a = "new Ajax.Request('/selectdata/new/get_ids?id=687420'";
php > preg_match('|new Ajax.Request\\(\'/selectdata/new/get_ids\\?id=(.+?)\'|', $a, $matches);
php > var_dump($matches);
array(2) {
  [0]=>
  string(52) "new Ajax.Request('/selectdata/new/get_ids?id=687420'"
  [1]=>
  string(6) "687420"
}