mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
Improve preg_* functions warnings for NUL byte (#13068)
* Improve error messages for preg_ functions * Adjusted tests and fixed formatting * Removed unnecessary strings from preg_* tests * Removed ZPP tests
This commit is contained in:
@@ -649,7 +649,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
|
||||
if (key != regex) {
|
||||
zend_string_release_ex(key, 0);
|
||||
}
|
||||
php_error_docref(NULL, E_WARNING, "Delimiter must not be alphanumeric, backslash, or NUL");
|
||||
php_error_docref(NULL, E_WARNING, "Delimiter must not be alphanumeric, backslash, or NUL byte");
|
||||
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
|
||||
return NULL;
|
||||
}
|
||||
@@ -745,7 +745,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
|
||||
if (pp[-1]) {
|
||||
php_error_docref(NULL, E_WARNING, "Unknown modifier '%c'", pp[-1]);
|
||||
} else {
|
||||
php_error_docref(NULL, E_WARNING, "NUL is not a valid modifier");
|
||||
php_error_docref(NULL, E_WARNING, "NUL byte is not a valid modifier");
|
||||
}
|
||||
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
|
||||
efree(pattern);
|
||||
|
||||
@@ -15,11 +15,21 @@ class Foo {
|
||||
function b() {
|
||||
return "b";
|
||||
}
|
||||
var_dump(preg_replace_callback_array(
|
||||
array(
|
||||
"/a/" => 'b', "/b/" => function () { return "c"; }, "/c/" => new Rep, "reporting" => array("Foo", "rep"), "a1" => array("Foo", "rep"),
|
||||
), 'a'));
|
||||
var_dump(
|
||||
preg_replace_callback_array(
|
||||
[
|
||||
"/a/" => 'b',
|
||||
"/b/" => function () {
|
||||
return 'c';
|
||||
},
|
||||
"/c/" => new Rep(),
|
||||
"reporting" => ["Foo", "rep"],
|
||||
"a1" => ["Foo", "rep"],
|
||||
],
|
||||
'a'
|
||||
)
|
||||
);
|
||||
?>
|
||||
--EXPECTF--
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL in %sbug73392.php on line %d
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL byte in %sbug73392.php on line %d
|
||||
NULL
|
||||
|
||||
@@ -23,7 +23,7 @@ Warning: preg_match(): Empty regular expression in %sdelimiters.php on line 4
|
||||
bool(false)
|
||||
int(1)
|
||||
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %sdelimiters.php on line 6
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %sdelimiters.php on line 6
|
||||
bool(false)
|
||||
int(1)
|
||||
|
||||
@@ -37,5 +37,5 @@ bool(false)
|
||||
Warning: preg_match(): No ending matching delimiter '}' found in %sdelimiters.php on line 11
|
||||
bool(false)
|
||||
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %sdelimiters.php on line 12
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %sdelimiters.php on line 12
|
||||
bool(false)
|
||||
|
||||
@@ -29,28 +29,28 @@ var_dump(preg_match("[abc\0def]", "abc\0fed"));
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %snull_bytes.php on line 3
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %snull_bytes.php on line 3
|
||||
bool(false)
|
||||
int(0)
|
||||
int(1)
|
||||
|
||||
Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 6
|
||||
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 6
|
||||
bool(false)
|
||||
|
||||
Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 7
|
||||
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 7
|
||||
bool(false)
|
||||
int(0)
|
||||
int(1)
|
||||
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %snull_bytes.php on line 11
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %snull_bytes.php on line 11
|
||||
bool(false)
|
||||
int(0)
|
||||
int(1)
|
||||
|
||||
Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 14
|
||||
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 14
|
||||
bool(false)
|
||||
|
||||
Warning: preg_match(): NUL is not a valid modifier in %snull_bytes.php on line 15
|
||||
Warning: preg_match(): NUL byte is not a valid modifier in %snull_bytes.php on line 15
|
||||
bool(false)
|
||||
int(0)
|
||||
int(1)
|
||||
|
||||
@@ -3,21 +3,25 @@ Test preg_grep() function : error conditions - bad regular expressions
|
||||
--FILE--
|
||||
<?php
|
||||
/*
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
/*
|
||||
* Testing how preg_grep reacts to being passed bad regexes
|
||||
*/
|
||||
echo "*** Testing preg_grep() : error conditions ***\n";
|
||||
$values = array('abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
|
||||
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
|
||||
);
|
||||
$array = array(123, 'abc', 'test');
|
||||
foreach($values as $value) {
|
||||
@print "\nArg value is $value\n";
|
||||
* Testing how preg_grep reacts to being passed bad regexes
|
||||
*/
|
||||
$values = [
|
||||
'abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F',
|
||||
[
|
||||
'[a-z]', //Array of Regexes
|
||||
'[A-Z]',
|
||||
'[0-9]',
|
||||
],
|
||||
'/[a-zA-Z]/', //Regex string
|
||||
];
|
||||
$array = [123, 'abc', 'test'];
|
||||
foreach ($values as $value) {
|
||||
try {
|
||||
var_dump(preg_grep($value, $array));
|
||||
} catch (TypeError $e) {
|
||||
@@ -30,35 +34,21 @@ try {
|
||||
} catch (TypeError $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing preg_grep() : error conditions ***
|
||||
|
||||
Arg value is abcdef
|
||||
|
||||
Warning: preg_grep(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_grep_error1.php on line %d
|
||||
Warning: preg_grep(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_grep_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is /[a-zA-Z]
|
||||
|
||||
Warning: preg_grep(): No ending delimiter '/' found in %spreg_grep_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is [a-zA-Z]/
|
||||
|
||||
Warning: preg_grep(): Unknown modifier '/' in %spreg_grep_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is /[a-zA-Z]/F
|
||||
|
||||
Warning: preg_grep(): Unknown modifier 'F' in %spreg_grep_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is Array
|
||||
preg_grep(): Argument #1 ($pattern) must be of type string, array given
|
||||
|
||||
Arg value is /[a-zA-Z]/
|
||||
array(2) {
|
||||
[1]=>
|
||||
string(3) "abc"
|
||||
@@ -66,4 +56,3 @@ array(2) {
|
||||
string(4) "test"
|
||||
}
|
||||
preg_grep(): Argument #1 ($pattern) must be of type string, stdClass given
|
||||
Done
|
||||
|
||||
@@ -3,21 +3,25 @@ Test preg_match_all() function : error conditions - bad regular expressions
|
||||
--FILE--
|
||||
<?php
|
||||
/*
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
/*
|
||||
* Testing how preg_match_all reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
echo "*** Testing preg_match_all() : error conditions ***\n";
|
||||
$regex_array = array('abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
|
||||
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
|
||||
);
|
||||
* Testing how preg_match_all reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
$regex_array = [
|
||||
'abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F',
|
||||
[
|
||||
'[a-z]', //Array of Regexes
|
||||
'[A-Z]',
|
||||
'[0-9]',
|
||||
],
|
||||
'/[a-zA-Z]/', //Regex string
|
||||
];
|
||||
$subject = 'test';
|
||||
foreach($regex_array as $regex_value) {
|
||||
@print "\nArg value is $regex_value\n";
|
||||
foreach ($regex_array as $regex_value) {
|
||||
try {
|
||||
var_dump(preg_match_all($regex_value, $subject, $matches1));
|
||||
} catch (TypeError $e) {
|
||||
@@ -34,37 +38,24 @@ try {
|
||||
var_dump($matches);
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing preg_match_all() : error conditions ***
|
||||
|
||||
Arg value is abcdef
|
||||
|
||||
Warning: preg_match_all(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_match_all_error1.php on line %d
|
||||
Warning: preg_match_all(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_match_all_error1.php on line %d
|
||||
bool(false)
|
||||
NULL
|
||||
|
||||
Arg value is /[a-zA-Z]
|
||||
|
||||
Warning: preg_match_all(): No ending delimiter '/' found in %spreg_match_all_error1.php on line %d
|
||||
bool(false)
|
||||
NULL
|
||||
|
||||
Arg value is [a-zA-Z]/
|
||||
|
||||
Warning: preg_match_all(): Unknown modifier '/' in %spreg_match_all_error1.php on line %d
|
||||
bool(false)
|
||||
NULL
|
||||
|
||||
Arg value is /[a-zA-Z]/F
|
||||
|
||||
Warning: preg_match_all(): Unknown modifier 'F' in %spreg_match_all_error1.php on line %d
|
||||
bool(false)
|
||||
NULL
|
||||
|
||||
Arg value is Array
|
||||
preg_match_all(): Argument #1 ($pattern) must be of type string, array given
|
||||
NULL
|
||||
|
||||
Arg value is /[a-zA-Z]/
|
||||
int(4)
|
||||
array(1) {
|
||||
[0]=>
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
--TEST--
|
||||
Test preg_match_all() function : error conditions - wrong arg types
|
||||
--FILE--
|
||||
<?php
|
||||
/*
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
/*
|
||||
* Testing how preg_match_all reacts to being passed the wrong type of input argument
|
||||
*/
|
||||
echo "*** Testing preg_match_all() : error conditions ***\n";
|
||||
$regex = '/[a-zA-Z]/';
|
||||
$input = array(array('this is', 'a subarray'), 'test',);
|
||||
foreach($input as $value) {
|
||||
@print "\nArg value is: $value\n";
|
||||
try {
|
||||
var_dump(preg_match_all($regex, $value, $matches));
|
||||
} catch (TypeError $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
var_dump($matches);
|
||||
}
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECT--
|
||||
*** Testing preg_match_all() : error conditions ***
|
||||
|
||||
Arg value is: Array
|
||||
preg_match_all(): Argument #2 ($subject) must be of type string, array given
|
||||
NULL
|
||||
|
||||
Arg value is: test
|
||||
int(4)
|
||||
array(1) {
|
||||
[0]=>
|
||||
array(4) {
|
||||
[0]=>
|
||||
string(1) "t"
|
||||
[1]=>
|
||||
string(1) "e"
|
||||
[2]=>
|
||||
string(1) "s"
|
||||
[3]=>
|
||||
string(1) "t"
|
||||
}
|
||||
}
|
||||
Done
|
||||
@@ -4,18 +4,22 @@ Test preg_match() function : error conditions - bad regular expressions
|
||||
<?php
|
||||
/* Function is implemented in ext/pcre/php_pcre.c */
|
||||
/*
|
||||
* Testing how preg_match reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
echo "*** Testing preg_match() : error conditions ***\n";
|
||||
$regex_array = array('abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
|
||||
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
|
||||
);
|
||||
* Testing how preg_match reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
$regex_array = [
|
||||
'abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F',
|
||||
[
|
||||
'[a-z]', //Array of Regexes
|
||||
'[A-Z]',
|
||||
'[0-9]',
|
||||
],
|
||||
'/[a-zA-Z]/', //Regex string
|
||||
];
|
||||
$subject = 'this is a test';
|
||||
foreach($regex_array as $regex_value) {
|
||||
@print "\nArg value is $regex_value\n";
|
||||
foreach ($regex_array as $regex_value) {
|
||||
try {
|
||||
var_dump(preg_match($regex_value, $subject));
|
||||
} catch (TypeError $e) {
|
||||
@@ -28,33 +32,21 @@ try {
|
||||
} catch (TypeError $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing preg_match() : error conditions ***
|
||||
|
||||
Arg value is abcdef
|
||||
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_match_error1.php on line %d
|
||||
Warning: preg_match(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_match_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is /[a-zA-Z]
|
||||
|
||||
Warning: preg_match(): No ending delimiter '/' found in %spreg_match_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is [a-zA-Z]/
|
||||
|
||||
Warning: preg_match(): Unknown modifier '/' in %spreg_match_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is /[a-zA-Z]/F
|
||||
|
||||
Warning: preg_match(): Unknown modifier 'F' in %spreg_match_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is Array
|
||||
preg_match(): Argument #1 ($pattern) must be of type string, array given
|
||||
|
||||
Arg value is /[a-zA-Z]/
|
||||
int(1)
|
||||
preg_match(): Argument #1 ($pattern) must be of type string, stdClass given
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
--TEST--
|
||||
Test preg_match() function : error conditions - wrong arg types
|
||||
--FILE--
|
||||
<?php
|
||||
/* Function is implemented in ext/pcre/php_pcre.c */
|
||||
/*
|
||||
* Testing how preg_match reacts to being passed the wrong type of subject argument
|
||||
*/
|
||||
echo "*** Testing preg_match() : error conditions ***\n";
|
||||
$regex = '/[a-zA-Z]/';
|
||||
$input = array('this is a string', array('this is', 'a subarray'),);
|
||||
foreach($input as $value) {
|
||||
@print "\nArg value is: $value\n";
|
||||
try {
|
||||
var_dump(preg_match($regex, $value));
|
||||
} catch (TypeError $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
}
|
||||
$value = new stdclass(); //Object
|
||||
try {
|
||||
var_dump(preg_match($regex, $value));
|
||||
} catch (TypeError $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECT--
|
||||
*** Testing preg_match() : error conditions ***
|
||||
|
||||
Arg value is: this is a string
|
||||
int(1)
|
||||
|
||||
Arg value is: Array
|
||||
preg_match(): Argument #2 ($subject) must be of type string, array given
|
||||
preg_match(): Argument #2 ($subject) must be of type string, stdClass given
|
||||
Done
|
||||
@@ -9,41 +9,83 @@ function b() {
|
||||
|
||||
// empty strings
|
||||
|
||||
var_dump(preg_replace_callback_array(
|
||||
array(
|
||||
"/a/" => 'b',
|
||||
"" => function () { return "ok"; }), 'a'));
|
||||
var_dump(
|
||||
preg_replace_callback_array(
|
||||
[
|
||||
'/a/' => 'b',
|
||||
'' => function () {
|
||||
return 'ok';
|
||||
},
|
||||
],
|
||||
'a'
|
||||
)
|
||||
);
|
||||
|
||||
var_dump(preg_replace_callback_array(
|
||||
array(
|
||||
"/a/" => 'b',
|
||||
null => function () { return "ok"; }), 'a'));
|
||||
var_dump(
|
||||
preg_replace_callback_array(
|
||||
[
|
||||
'/a/' => 'b',
|
||||
null => function () {
|
||||
return 'ok';
|
||||
},
|
||||
],
|
||||
'a'
|
||||
)
|
||||
);
|
||||
|
||||
// backslashes
|
||||
|
||||
var_dump(preg_replace_callback_array(
|
||||
array(
|
||||
"/a/" => 'b',
|
||||
"\\b\\" => function () { return "ok"; }), 'a'));
|
||||
var_dump(
|
||||
preg_replace_callback_array(
|
||||
[
|
||||
'/a/' => 'b',
|
||||
"\\b\\" => function () {
|
||||
return 'ok';
|
||||
},
|
||||
],
|
||||
'a'
|
||||
)
|
||||
);
|
||||
|
||||
// alphanumeric delimiters
|
||||
|
||||
var_dump(preg_replace_callback_array(
|
||||
array(
|
||||
"/a/" => 'b',
|
||||
"aba" => function () { return "ok"; }), 'a'));
|
||||
var_dump(
|
||||
preg_replace_callback_array(
|
||||
[
|
||||
'/a/' => 'b',
|
||||
'aba' => function () {
|
||||
return 'ok';
|
||||
},
|
||||
],
|
||||
'a'
|
||||
)
|
||||
);
|
||||
|
||||
var_dump(preg_replace_callback_array(
|
||||
array(
|
||||
"/a/" => 'b',
|
||||
"1b1" => function () { return "ok"; }), 'a'));
|
||||
var_dump(
|
||||
preg_replace_callback_array(
|
||||
[
|
||||
'/a/' => 'b',
|
||||
'1b1' => function () {
|
||||
return 'ok';
|
||||
},
|
||||
],
|
||||
'a'
|
||||
)
|
||||
);
|
||||
|
||||
// null character delimiter
|
||||
|
||||
var_dump(preg_replace_callback_array(
|
||||
array(
|
||||
"/a/" => 'b',
|
||||
"\0b\0" => function () { return "ok"; }), 'a'));
|
||||
var_dump(
|
||||
preg_replace_callback_array(
|
||||
[
|
||||
'/a/' => 'b',
|
||||
"\0b\0" => function () {
|
||||
return 'ok';
|
||||
},
|
||||
],
|
||||
'a'
|
||||
)
|
||||
);
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
@@ -53,14 +95,14 @@ NULL
|
||||
Warning: preg_replace_callback_array(): Empty regular expression in %spreg_replace_callback_array_error.php on line %d
|
||||
NULL
|
||||
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_replace_callback_array_error.php on line %d
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_replace_callback_array_error.php on line %d
|
||||
NULL
|
||||
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_replace_callback_array_error.php on line %d
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_replace_callback_array_error.php on line %d
|
||||
NULL
|
||||
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_replace_callback_array_error.php on line %d
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_replace_callback_array_error.php on line %d
|
||||
NULL
|
||||
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_replace_callback_array_error.php on line %d
|
||||
NULL
|
||||
Warning: preg_replace_callback_array(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_replace_callback_array_error.php on line %d
|
||||
NULL
|
||||
|
||||
@@ -3,53 +3,56 @@ Test preg_replace_callback() function : error
|
||||
--FILE--
|
||||
<?php
|
||||
/*
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
/*
|
||||
* Testing how preg_replace_callback reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
echo "*** Testing preg_replace_callback() : error conditions ***\n";
|
||||
$regex_array = array('abcdef', //Regex without delimiters
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
|
||||
'[A-Z]', '[0-9]'), '/[0-9]/'); //Regex string
|
||||
$replacement = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
|
||||
* Testing how preg_replace_callback reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
$regex_array = [
|
||||
'abcdef', //Regex without delimiters
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F',
|
||||
[
|
||||
'[a-z]', //Array of Regexes
|
||||
'[A-Z]',
|
||||
'[0-9]',
|
||||
],
|
||||
'/[0-9]/', //Regex string
|
||||
];
|
||||
$replacement = [
|
||||
'zero',
|
||||
'one',
|
||||
'two',
|
||||
'three',
|
||||
'four',
|
||||
'five',
|
||||
'six',
|
||||
'seven',
|
||||
'eight',
|
||||
'nine',
|
||||
];
|
||||
function integer_word($matches) {
|
||||
global $replacement;
|
||||
return $replacement[$matches[0]];
|
||||
}
|
||||
$subject = 'number 1.';
|
||||
foreach($regex_array as $regex_value) {
|
||||
@print "\nArg value is $regex_value\n";
|
||||
foreach ($regex_array as $regex_value) {
|
||||
var_dump(preg_replace_callback($regex_value, 'integer_word', $subject));
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing preg_replace_callback() : error conditions ***
|
||||
|
||||
Arg value is abcdef
|
||||
|
||||
Warning: preg_replace_callback(): Delimiter must not be alphanumeric, backslash, or NUL in %s on line %d
|
||||
Warning: preg_replace_callback(): Delimiter must not be alphanumeric, backslash, or NUL byte in %s on line %d
|
||||
NULL
|
||||
|
||||
Arg value is /[a-zA-Z]
|
||||
|
||||
Warning: preg_replace_callback(): No ending delimiter '/' found in %s on line %d
|
||||
NULL
|
||||
|
||||
Arg value is [a-zA-Z]/
|
||||
|
||||
Warning: preg_replace_callback(): Unknown modifier '/' in %s on line %d
|
||||
NULL
|
||||
|
||||
Arg value is /[a-zA-Z]/F
|
||||
|
||||
Warning: preg_replace_callback(): Unknown modifier 'F' in %s on line %d
|
||||
NULL
|
||||
|
||||
Arg value is Array
|
||||
string(9) "number 1."
|
||||
|
||||
Arg value is /[0-9]/
|
||||
string(11) "number one."
|
||||
|
||||
@@ -3,22 +3,26 @@ Test preg_replace() function : error - bad regular expressions
|
||||
--FILE--
|
||||
<?php
|
||||
/*
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
/*
|
||||
* Testing how preg_replace reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
echo "*** Testing preg_replace() : error conditions***\n";
|
||||
$regex_array = array('abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
|
||||
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
|
||||
);
|
||||
* Testing how preg_replace reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
$regex_array = [
|
||||
'abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F',
|
||||
[
|
||||
'[a-z]', //Array of Regexes
|
||||
'[A-Z]',
|
||||
'[0-9]',
|
||||
],
|
||||
'/[a-zA-Z]/', //Regex string
|
||||
];
|
||||
$replace = 1;
|
||||
$subject = 'a';
|
||||
foreach($regex_array as $regex_value) {
|
||||
@print "\nArg value is $regex_value\n";
|
||||
foreach ($regex_array as $regex_value) {
|
||||
var_dump(preg_replace($regex_value, $replace, $subject));
|
||||
}
|
||||
$regex_value = new stdclass(); //Object
|
||||
@@ -29,31 +33,18 @@ try {
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing preg_replace() : error conditions***
|
||||
|
||||
Arg value is abcdef
|
||||
|
||||
Warning: preg_replace(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_replace_error1.php on line %d
|
||||
Warning: preg_replace(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_replace_error1.php on line %d
|
||||
NULL
|
||||
|
||||
Arg value is /[a-zA-Z]
|
||||
|
||||
Warning: preg_replace(): No ending delimiter '/' found in %spreg_replace_error1.php on line %d
|
||||
NULL
|
||||
|
||||
Arg value is [a-zA-Z]/
|
||||
|
||||
Warning: preg_replace(): Unknown modifier '/' in %spreg_replace_error1.php on line %d
|
||||
NULL
|
||||
|
||||
Arg value is /[a-zA-Z]/F
|
||||
|
||||
Warning: preg_replace(): Unknown modifier 'F' in %spreg_replace_error1.php on line %d
|
||||
NULL
|
||||
|
||||
Arg value is Array
|
||||
string(1) "a"
|
||||
|
||||
Arg value is /[a-zA-Z]/
|
||||
string(1) "1"
|
||||
preg_replace(): Argument #1 ($pattern) must be of type array|string, stdClass given
|
||||
|
||||
@@ -8,12 +8,10 @@ Test preg_replace() function : error conditions - wrong arg types
|
||||
/*
|
||||
* Testing how preg_replace reacts to being passed the wrong type of replacement argument
|
||||
*/
|
||||
echo "*** Testing preg_replace() : error conditions ***\n";
|
||||
$regex = '/[a-zA-Z]/';
|
||||
$replace = array('this is a string', array('this is', 'a subarray'),);
|
||||
$subject = 'test';
|
||||
foreach($replace as $value) {
|
||||
@print "\nArg value is: $value\n";
|
||||
try {
|
||||
var_dump(preg_replace($regex, $value, $subject));
|
||||
} catch (TypeError $e) {
|
||||
@@ -26,15 +24,8 @@ try {
|
||||
} catch (Error $e) {
|
||||
echo $e->getMessage(), "\n";
|
||||
}
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECT--
|
||||
*** Testing preg_replace() : error conditions ***
|
||||
|
||||
Arg value is: this is a string
|
||||
string(64) "this is a stringthis is a stringthis is a stringthis is a string"
|
||||
|
||||
Arg value is: Array
|
||||
preg_replace(): Argument #1 ($pattern) must be of type array when argument #2 ($replacement) is an array, string given
|
||||
preg_replace(): Argument #2 ($replacement) must be of type array|string, stdClass given
|
||||
Done
|
||||
|
||||
@@ -3,21 +3,25 @@ Test preg_split() function : error conditions - bad regular expressions
|
||||
--FILE--
|
||||
<?php
|
||||
/*
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
* Function is implemented in ext/pcre/php_pcre.c
|
||||
*/
|
||||
/*
|
||||
* Testing how preg_split reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
echo "*** Testing preg_split() : error conditions ***\n";
|
||||
$regex_array = array('abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
|
||||
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
|
||||
);
|
||||
* Testing how preg_split reacts to being passed the wrong type of regex argument
|
||||
*/
|
||||
$regex_array = [
|
||||
'abcdef', //Regex without delimiter
|
||||
'/[a-zA-Z]', //Regex without closing delimiter
|
||||
'[a-zA-Z]/', //Regex without opening delimiter
|
||||
'/[a-zA-Z]/F',
|
||||
[
|
||||
'[a-z]', //Array of Regexes
|
||||
'[A-Z]',
|
||||
'[0-9]',
|
||||
],
|
||||
'/[a-zA-Z]/', //Regex string
|
||||
];
|
||||
$subject = '1 2 a 3 4 b 5 6';
|
||||
foreach($regex_array as $regex_value) {
|
||||
@print "\nArg value is $regex_value\n";
|
||||
foreach ($regex_array as $regex_value) {
|
||||
try {
|
||||
var_dump(preg_split($regex_value, $subject));
|
||||
} catch (TypeError $e) {
|
||||
@@ -32,32 +36,19 @@ try {
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing preg_split() : error conditions ***
|
||||
|
||||
Arg value is abcdef
|
||||
|
||||
Warning: preg_split(): Delimiter must not be alphanumeric, backslash, or NUL in %spreg_split_error1.php on line %d
|
||||
Warning: preg_split(): Delimiter must not be alphanumeric, backslash, or NUL byte in %spreg_split_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is /[a-zA-Z]
|
||||
|
||||
Warning: preg_split(): No ending delimiter '/' found in %spreg_split_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is [a-zA-Z]/
|
||||
|
||||
Warning: preg_split(): Unknown modifier '/' in %spreg_split_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is /[a-zA-Z]/F
|
||||
|
||||
Warning: preg_split(): Unknown modifier 'F' in %spreg_split_error1.php on line %d
|
||||
bool(false)
|
||||
|
||||
Arg value is Array
|
||||
preg_split(): Argument #1 ($pattern) must be of type string, array given
|
||||
|
||||
Arg value is /[a-zA-Z]/
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(4) "1 2 "
|
||||
|
||||
Reference in New Issue
Block a user