PHP中如何检查字符串是否可以用作变量名?
在 PHP 中,可以使用可变变量...
例如...
class obj { }
$fieldName = "Surname";
$object = new obj();
$object->Name = "John";
$object->$fieldName = "Doe";
echo "{$object->Name} {$object->Surname}"; // This echoes "John Doe".
但是,$fieldName 字符串可能包含一些变量名称中不允许的字符。 PHP 仍将创建具有该名称的字段(很像关联数组),但我将无法使用 $object->...... 访问它,因为它无法正确解析。
现在,是否有任何函数可以检查该字符串是否可以用作有效的 PHP 变量名。如果没有,如何使用正则表达式创建它? PHP 中变量命名的规则是什么?
In PHP one can use variable variables...
For example...
class obj { }
$fieldName = "Surname";
$object = new obj();
$object->Name = "John";
$object->$fieldName = "Doe";
echo "{$object->Name} {$object->Surname}"; // This echoes "John Doe".
However, $fieldName string may contain some characters not allowed in variable names. PHP will still create the field with that name (much like the associative array), but I will not be able to access it with $object->...... because it would not parse correctly.
Now, is there any function that can check if the string can be used as a valid PHP variable name. If not, how would this be created using regular expressions? What are the rules for variable names in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自手册:
因此,如果您通过正则表达式运行你的字符串,你应该能够判断它是否有效。
应该注意的是,使用可变变量访问“无效”对象属性名称的能力是某些 XML 解析的正确方法。
例如,来自
SimpleXML
文档:接下来是这个代码示例:
因此,拥有只能通过这种方式访问的属性不一定错误。
但是,如果您的代码既创建又使用该对象 - 人们会想知道为什么要使用这些类型的属性。当然,允许出现类似于
SimpleXML
示例的情况,其中创建一个对象来表示控件范围之外的内容。From the manual:
So If you ran your string through the RegEx, you should be able to tell if it's valid or not.
It should be noted that the ability to access 'invalid' Object property names using a variable variable is the correct approach for some XML parsing.
For example, from the
SimpleXML
docs:Followed by this code example:
So it's not necessarily wrong to have properties that can only be accessed this way.
However, if your code both creates and uses the object - one would wonder why you would use those kind of properties. Allowing, of course, a situation similar to the
SimpleXML
example, where an object is created to represent something outside the scope of your control.因为它已经回复了,但没有完整的代码行:
As it was already replied, but not with a complete line of code:
'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
将验证 PHP 变量名称。该正则表达式直接来自以下文档:http://www.php。 net/manual/en/language.variables.basics.php
'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
will validate a PHP variable name.This regex is directly from the documentation at: http://www.php.net/manual/en/language.variables.basics.php
您仍然可以通过
$object->{"fieldname"}
语法访问该字段。据我所知,唯一的限制是您无法访问名称中包含
\x00
的属性,并且无法定义以\x00
开头的变量。例子:
You will still be able to access the field through the
$object->{"fieldname"}
syntax.As far as I know, the only restriction is that you can't access properties with
\x00
in the name and you can't define variables starting with\x00
.Example:
,但看起来:
当然不推荐,但它可以完成。
but look:
Certainly not recommended but it can be done.
我认为正则表达式是可行的方法,据我所知,限制是:
因此正则表达式将为“/[a-zA-Z]+[0-9a- zA-Z_]*/" - 从我的头顶上下来,所以你的里程可能会有所不同。
I think regex is the way to go, and as far as I can remember the restrictions are:
so the regex would be "/[a-zA-Z]+[0-9a-zA-Z_]*/" - off the top of my head so your milage may vary.
使用 RegEx 进行验证 如果您希望允许在字符串中验证
$
或&$
(通过引用传递变量),您可以使用以下正则表达式:Validating with RegEx if you wanted to allow
$
or&$
(pass variable by reference) to be validated in the string, you could use this regex: