PHP中如何检查字符串是否可以用作变量名?

发布于 2024-09-28 15:21:59 字数 451 浏览 9 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

原来是傀儡 2024-10-05 15:21:59

来自手册

变量名称遵循与 PHP 中其他标签相同的规则。有效的变量名称以字母或下划线开头,后跟任意数量的字母、数字或下划线。作为正则表达式,它可以表示为:'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

因此,如果您通过正则表达式运行你的字符串,你应该能够判断它是否有效。

应该注意的是,使用可变变量访问“无效”对象属性名称的能力是某些 XML 解析的正确方法。

例如,来自 SimpleXML 文档:

访问 XML 文档中包含 PHP 命名约定不允许的字符(例如连字符)的元素可以通过将元素名称封装在大括号和撇号内来完成。

接下来是这个代码示例:

echo $xml->movie->{'great-lines'}->line;

因此,拥有只能通过这种方式访问​​的属性不一定错误

但是,如果您的代码既创建又使用该对象 - 人们会想知道为什么要使用这些类型的属性。当然,允许出现类似于 SimpleXML 示例的情况,其中创建一个对象来表示控件范围之外的内容。

From the manual:

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

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:

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

Followed by this code example:

echo $xml->movie->{'great-lines'}->line;

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.

静若繁花 2024-10-05 15:21:59

因为它已经回复了,但没有完整的代码行:

if ( preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/','evaluation string') )
{
    // the string is valid
}
else
{
    // the string is not valid
}

As it was already replied, but not with a complete line of code:

if ( preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/','evaluation string') )
{
    // the string is valid
}
else
{
    // the string is not valid
}
对风讲故事 2024-10-05 15:21:59

'[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

梦里的微风 2024-10-05 15:21:59

但是,`$fieldName 字符串可能包含一些变量名称中不允许的字符。 PHP 仍将创建具有该名称的字段(很像关联数组),但我将无法使用 $object->...... 访问它,因为它无法正确解析。

您仍然可以通过 $object->{"fieldname"} 语法访问该字段。

据我所知,唯一的限制是您无法访问名称中包含 \x00 的属性,并且无法定义以 \x00 开头的变量。

例子:

$a = new stdclass;
$a->{"\x00dd"} = 8; //invalid
$a->{"dd\x00dd"} = 8; //valid, but...
echo $a->{"dd\x00dd"}; //can't read (no property "dd")

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.

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:

$a = new stdclass;
$a->{"\x00dd"} = 8; //invalid
$a->{"dd\x00dd"} = 8; //valid, but...
echo $a->{"dd\x00dd"}; //can't read (no property "dd")
待"谢繁草 2024-10-05 15:21:59

但我无法访问它
与 $object->...... 因为它会
无法正确解析

,但看起来:

class A {}

$varName = '!asd asd';
$a = new A();
$a->$varName = '1';
echo "{$a->{'!asd asd'}}"; // 1

当然不推荐,但它可以完成。

but I will not be able to access it
with $object->...... because it would
not parse correctly

but look:

class A {}

$varName = '!asd asd';
$a = new A();
$a->$varName = '1';
echo "{$a->{'!asd asd'}}"; // 1

Certainly not recommended but it can be done.

奈何桥上唱咆哮 2024-10-05 15:21:59

我认为正则表达式是可行的方法,据我所知,限制是:

  • 字母数字
  • 必须以字母开头
  • 可以包含下划线,

因此正则表达式将为“/[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:

  • alphanumeric
  • must start with a letter
  • can contain an underscore

so the regex would be "/[a-zA-Z]+[0-9a-zA-Z_]*/" - off the top of my head so your milage may vary.

流年已逝 2024-10-05 15:21:59

使用 RegEx 进行验证 如果您希望允许在字符串中验证 $&$ (通过引用传递变量),您可以使用以下正则表达式:

/^([\$]|(&\$))[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/

Validating with RegEx if you wanted to allow $ or &$ (pass variable by reference) to be validated in the string, you could use this regex:

/^([\$]|(&\$))[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文