问题描述
我需要将以下 perl 函数转换为 php:
I need to convert the following perl function to php:
pack("SSA12AC4L",
$id,
$loc,
$name,
'ar',
split(/\./, $get->getIP),
time+(60*60);
我在 PHP 中使用以下代码(用于测试):
I use the following code (to test) in PHP:
echo pack("SSA12AC4L",
'25',
'00001',
'2u7wx6fd94fd',
'f',
preg_split('/\./','10.2.1.1', -1, PREG_SPLIT_NO_EMPTY),
time()+(60*60));
但我收到以下错误:警告:pack() [function.pack]:Type C:第 8 行 D:\wamp\www\test.php 中的参数太少
But I'm getting the following error:Warning: pack() [function.pack]: Type C: too few arguments in D:\wamp\www\test.php on line 8
有什么建议吗?非常感谢.
Any suggestions? Thanks a lot.
推荐答案
问题是代码给了 pack()
(我指的是最后一个参数)一个字符,一个数组, 和一个整数.由于代码 C
需要 4 个字符,这就是错误的原因.
The problem is that the code is giving to pack()
(I am referring the the last arguments) a character, an array, and an integer. As the code C
wants 4 characters, that is the cause of the error.
代码应该类似于
$split = preg_split('/\./','10.2.1.1', -1, PREG_SPLIT_NO_EMPTY);
echo pack("SSA12AC4L",
'25',
'00001',
'2u7wx6fd94fd',
'f',
$split[0],
$split[1],
$split[2],
$split[3],
time()+60*60
);
那么,在这种情况下没有理由使用 preg_split()
,当可以使用 explode()
代替时.正则表达式应仅在绝对必要时使用,因为与其他字符串函数相比,正则表达式函数速度较慢.
Then, there is no reason to use preg_split()
in this case, when explode()
can be used instead. Regular expressions should be used only when strictly necessary because the regular expression functions are slower, compared to other string functions.
这篇关于将 Perl 代码转换为 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!