0

I have a regex /id=?\d*-\d*/ i tested it here live demo

but when i use this code for getting id=3607-7

$line = '[24/Oct/2016:11:20:57 +0200] "GET /ajax/magasins/initMagasin.php?id=3607-7 HTTP/1.1" 200 747 "https://asdjjhdsa.fr/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Safari/602.1.50';
preg_match('id=?\d*-\d*', $line, $matches);
echo $matches[1] ."\n"; 

i have this warning

PHP Warning: preg_match(): Delimiter must not be alphanumeric or backslash

I read this question and when i use

preg_match('/id=?\d*-\d*/', $line, $matches);

i have this notice

PHP Notice: Undefined offset:

Community
  • 1
  • 1
parik
  • 2,313
  • 12
  • 39
  • 67
  • `preg_match('/id=?\d*-\d*/', $line, $matches);` – Wiktor Stribiżew Dec 21 '16 at 12:02
  • @WiktorStribiżew i edited my question, your answer give me a notice – parik Dec 21 '16 at 12:06
  • You malformed the string literal, use `$line = '[24/Oct/2016:11:20:57 +0200] "GET /ajax/magasins/initMagasin.php?id=3607-7 HTTP/1.1" 200 747 "https://asdjjhdsa.fr/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Safari/602.1.50';`. See [this demo](https://ideone.com/zjaf9f). – Wiktor Stribiżew Dec 21 '16 at 12:07
  • @WiktorStribiżew i had the same result when i used your $line – parik Dec 21 '16 at 12:10
  • 1
    You have no capture groups so the 1 index will never be populated. `echo $matches[0] `.... or group the `id` part so `1` is the integer, `id=?(\d*-\d*)` – chris85 Dec 21 '16 at 12:17
  • @chris85 YES, this is the answer, can you post it like an answer please? – parik Dec 21 '16 at 12:19
  • 1
    I can't, the question is closed. It'll just live as a comment answer. The dup is correct for the original question. – chris85 Dec 21 '16 at 12:21
  • 1
    In my demo above, I used `print_r($matches[0]);` to show which exact group to print. – Wiktor Stribiżew Dec 21 '16 at 12:43
  • @WiktorStribiżew Yes, the problem was matches[0], it was my fault, i didn't pay attention, thanks for your answer – parik Dec 21 '16 at 13:51

0 Answers0