Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: preg_match - "Delimiter must not be alphanumeric or backslash" [duplicate]

Tags:

regex

php

Does anyone know what is wrong with this regex? It works fine on sites like RegexPal and RegExr, but in PHP it gives me this warning and no results:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

Here's my code:

preg_match('name="dsh" id="dsh" value="(.*?)"', 'name="dsh" id="dsh" value="123"', $matches);
like image 352
Stan Avatar asked Dec 16 '22 03:12

Stan


2 Answers

You have no delimiter. Enclose the pattern in /

preg_match('/name="dsh" id="dsh" value="(.*?)"/', 'name="dsh" id="dsh" value="123"', $matches);

For patterns that include / on their own, it is advisable to use a different delimiter like ~ or # to avoid escaping:

// Delimited with # instead of /
preg_match('#name="dsh" id="dsh" value="(.*?)"#', 'name="dsh" id="dsh" value="123"', $matches);
like image 67
Michael Berkowski Avatar answered Jan 12 '23 00:01

Michael Berkowski


You need delimiters:

preg_match('/name="dsh" id="dsh" value="(.*?)"/', 'name="dsh" id="dsh" value="123"', $matches);
like image 45
Paul Avatar answered Jan 12 '23 00:01

Paul