Troubleshooting Delimiter must not be alphanumeric or backslash error when changing ereg() to preg_match()
ereg
is deprecated. Don't use it.- 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
Mcrypt Is Deprecated, What Is the Alternative
How to Generate in PHP All Combinations of Items in Multiple Arrays
How to Use Ajax in a Wordpress Shortcode
Facebook Graph API Not Work from 2.2 to 2.3
"Keep Me Logged In" - the Best Approach
How to Sort Files by Date in PHP
How to Prevent Browser Cache For PHP Site
How to Get Numeric Types from MySQL Using Pdo
Calculate Math Expression from a String Using Eval
PHP Elvis Operator VS Null Coalescing Operator
Recursive Function to Generate Multidimensional Array from Database Result
How to Savehtml of Domdocument Without HTML Wrapper
Accessing $_Cookie Immediately After Setcookie()
PHP - Floating Number Precision
"Fatal Error: Cannot Redeclare ≪Function≫"