Delimiter Must Not Be Alphanumeric or Backslash and Preg_Match

Troubleshooting Delimiter must not be alphanumeric or backslash error when changing ereg() to preg_match()

  1. ereg is deprecated. Don't use it.
  2. The preg functions are all "Perl regular expressions" meaning you need to have some sort of beginning and end marker on your regex. Often this will be / or #, but any non alpha-numeric will do fine.

For example, these will work:

preg_match("/foo/u",$needle,$haystack);
preg_match("#foo#i",$needle,$haystack);
preg_match("@foo@",$needle,$haystack);
preg_match("\$foo\$w",$needle,$haystack); // bad idea because `$` means something
// in regex but it is valid anyway
// also, they need to be escaped since
// I'm using " instead of '

But this will not:

preg_match("foo",$needle,$haystack); // no delimiter!

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

You have it the wrong way around. The pattern comes before. Try this:

$return = preg_match("/^[a-zA-Z ]+$/", $string);

Also, the docs can be read here.

PHP: preg_match - Delimiter must not be alphanumeric or backslash

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);

preg_match(): Delimiter must not be alphanumeric or backslash with custom field check

preg_match expects the paremeters in this order: preg_match( $pattern, $string). Also the pattern needs e.g. forward slashes as said delimiters. So a possible solution would be:

...
|| ($set['condition']['value']['operator'] == 'contains'
&& preg_match('/' . $custom_field_value . '/', $set['condition']['value']['value'] ))
|| ($set['condition']['value']['operator'] == 'does_not_contain'
&& !preg_match('/' . $custom_field_value . '/', $set['condition']['value']['value'] ))
...

If you want to do a simple check, if a string is inside the custom field i would suggest using strpos for performance reasons:

...
|| ($set['condition']['value']['operator'] == 'contains'
&& strpos($set['condition']['value']['value'],$custom_field_value) > 0)
|| ($set['condition']['value']['operator'] == 'does_not_contain'
&& strpos($set['condition']['value']['value'],$custom_field_value) == FALSE)
...

Regex results in Delimiter must not be alphanumeric or backslash

you must put pattern delimiters at the begining and at the end of the pattern, example:

$pattern = '#class="(.+)"(.+)<a.+>(.+)</a>#';

Here # is a better choice than / because you avoid to escape all the slashes inside your pattern, but you can write:

$pattern = '/class="(.+)"(.+)<a.+>(.+)<\/a>/';

As an aside comment, your pattern will cause many backtracks:

$pattern = '~class="([^"]+)"([^>]*>)<a[^>]+>([^<]+)</a>~';

will work better.

Keep in mind that + and * are by default greedy quantifiers (i.e. they take all they can).

If I use a restricted character class instead of the dot, I can stop the greediness of the quantifiers, example

[^"]+ take all characters except ", thus it stop when a " is find.

Demo:

<?php
function a($menu_item, $remove_link) {
//$pattern = '~class="(.+)"(.+)<a.+>(.+)</a>~';
$pattern = '~class="([^"]+)"([^>]*>)<a[^>]+>([^<]+)<\/a>~';
if($remove_link) {
return preg_replace($pattern, 'class="$1 selected"$2$3', $menu_item);
}
return $menu_item;
}

$menu_item = '<li class="menuitem first"><a href="index.php">Home</a></li>';
echo a($menu_item, true);

preg_replace() warning : Delimiter must not be alphanumeric or backslash

String concatenation in php is handled with a period . not a plus +:

$pageParticulier->post_content = preg_replace(
"#>" . $old_instance['title'] . "<#",
">" . $new_instance['title'] . "<",
$pageParticulier->post_content
);


Related Topics



Leave a reply



Submit