note 60355 deleted from language.functions by googleguy
| From: | googleguy@php.net | Date: | Mon, 24 Aug 2015 10:00:15 +0000 |
| Subject: | note 60355 deleted from language.functions by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-203429@lists.php.net to get a copy of this message | ||
Note Submitter: gnirts dot REMOVE_THIS at HATE_SPAM dot gmail dot com
----
If you want to validate that a string could be a valid function name, watch out. preg_match()
matches anywhere inside the test string, so strings like 'foo#' and ' bar'
will pass with the regex that they give ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
The solution is to use anchors in the regex (^ and $) and check the offset of the match (using
PREG_OFFSET_CAPTURE).
<?
function is_valid_function_name( $function_name_to_test )
{
$number_of_matches = preg_match( '<^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$>'
, $function_name_to_test
, $match_offset_array
, PREG_OFFSET_CAPTURE );
if( $number_of_matches === false )
{
trigger_error( 'Error with preg_match' , E_USER_WARNING );
}
if( $match_offset_array[0][1] !== 0 || $number_of_matches !== 1 )
{
return false;
}
else
{
return true;
}
}
?>