将 Perl 代码转换为 PHP

编程入门 行业动态 更新时间:2024-10-28 17:25:31
本文介绍了将 Perl 代码转换为 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要将以下 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

本文发布于:2023-10-07 15:55:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   代码   Perl   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!