解析表达式之间没有分隔符的 key=value 赋值字符串
我在 PHP 中有一个如下所示的字符串
$data = "ID=53KEY=23";
,我想将该字符串中的值分配给以下变量,
$id = 53;
$key = 23;
如何在 PHP 中执行此操作?
I have a string in PHP like the following
$data = "ID=53KEY=23";
and I want to assign the values from this string to following variables
$id = 53;
$key = 23;
How can I do this in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此函数适用于更通用的键/值输入,而不仅仅是 ID/KEY
This function will work for more generic key/value inputs, not just ID/KEY
对于更通用的解决方案:
For a more generic solution:
试试这个:
Try this:
没有可选表达式的可预测格式的字符串可以使用 sscanf() 进行解析,并且数值可以显式转换为数值类型。在这种情况下,使用
%
将数字转换为 int 类型值。代码:(演示)
输出:
Predictably formatted strings with no optional expressions can be parsed with
sscanf()
and numeric values can be explicitly cast as numeric types. In this case, use%
to cast numbers as int type values.Code: (Demo)
Output: