I am trying validate an array of strings to see if they are valid variable names, and if not throw an error. But using my test array (seen below), all keys come back as valid names, and I know that they shouldn't because the first one starts with a 3 which isn't valid. Am I doing something wrong?
foreach($key as $k => $v){
if(!preg_match("'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'", $k)){
throw new Exception("Invalid Meta Data Key Name");
}
$this->meta->$k = $v;
}
Regular Expression found here: http://php.net/manual/en/language.variables.basics.php
Here is the array that I am using to test the above:
$key = array(
"3total" => $this->foundRows,
"rows" => $this->resultSetSize,
"page" => $this->page,
"pages" => $this->getPages(),
"offset" => $this->getOffset(),
"columns" => $this->columns,
)
^$anchors, so your expression matches anywhere inside the string. It isn't enforcing letters at the beginning. Single quotes'are an unusual choice for regex delimiters, but I suppose if it's working for you... Something like/or~would be more conventional.