note 60355 added to language.functions

From: Date: Wed, 04 Jan 2006 18:11:02 +0000
Subject: note 60355 added to language.functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-101211@lists.php.net to get a copy of this message
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; } } ?> ---- Manual Page -- http://www.php.net/manual/en/language.functions.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60355 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60355&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60355&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60355&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60355&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60355&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60355&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60355&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60355&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#101211) next »