没写过 PHP ,临时需要,有 V 友能帮忙把这 20 行 JavaScript 移植到 PHP 吗?

2022-10-11 02:11:29 +08:00
 edis0n0
function convert(input = "") {
    let pdst = input.toUpperCase().trim();
    let pit = [];
    let isg = "";
    while (/[0-9A-Z]+/.exec(pdst) !== null) {
        let data = /[0-9A-Z]+/.exec(pdst);
        if (data !== null) {
            let pd = data[0];
            let pdt = pd.length;
            if (/[0-9]+/.test(pd) && pd.length < 8) isg += pd;
            if (isg.length == 15) pd = isg;
            if (isg.length >= 15) isg = "";
            if (/[0-9]+/.test(pd) && pd.length == 11 && pd.substring(0, 1).toLowerCase() == "s") pit.push(pd.trim().substring(1, 11));
            if (/[0-9]+/.test(pd) && pd.length == 13 && pd.substring(0, 1).toLowerCase() == "s") pit.push(pd.trim().substring(1, 13));
            if ((/[0-9]+/.test(pd) && pd.length == 15) || pd.length == 12 || pd.length == 10 || (/[0-9]+/.test(pd) && pd.length == 8)) pit.push(pd);
            pdst = pdst.substring(pdt).trim();
        }
    }
    return Array.from(new Set(pit));
}
5384 次点击
所在节点    PHP
32 条回复
ForkGagHub
2022-10-11 15:17:08 +08:00
虽然我不会拍黄片,但是帮你找到了一个网站
https://wtools.io/javascript-to-php-converter

<?php
$var = 'function convert(input = "") {
let pdst = input.toUpperCase().trim();
let pit = [];
let isg = "";
while (/[0-9A-Z]+/.exec(pdst) !== null) {
let data = /[0-9A-Z]+/.exec(pdst);
if (data !== null) {
let pd = data[0];
let pdt = pd.length;
if (/[0-9]+/.test(pd) && pd.length < 8) isg += pd;
if (isg.length == 15) pd = isg;
if (isg.length >= 15) isg = "";
if (/[0-9]+/.test(pd) && pd.length == 11 && pd.substring(0, 1).toLowerCase() == "s") pit.push(pd.trim().substring(1, 11));
if (/[0-9]+/.test(pd) && pd.length == 13 && pd.substring(0, 1).toLowerCase() == "s") pit.push(pd.trim().substring(1, 13));
if ((/[0-9]+/.test(pd) && pd.length == 15) || pd.length == 12 || pd.length == 10 || (/[0-9]+/.test(pd) && pd.length == 8)) pit.push(pd);
pdst = pdst.substring(pdt).trim();
}
}
return Array.from(new Set(pit));
}
l9rw
2022-10-11 15:26:11 +08:00
楼上工具转出来的代码第五行的判断都是错的
edis0n0
2022-10-11 15:54:06 +08:00
@l9rw #22 对,楼上转出来的没有一个能工作,在翻文档找问题
Austaras
2022-10-11 16:27:24 +08:00
你真要并发的话不如在 node 里面开个 worker ,反正都是原生数据类型
Light3
2022-10-11 16:31:11 +08:00
会写 php 但是 js 会的不多
没看懂楼主接了个字符串 为什么要循环字符串..

给楼主指一下 如何快速写出来
字符串 用 str_split() 分成数组
然后 foreach($list as $k=>$v){

} 循环

在循环里面 $k+1 就是字符串的第几位 然后判断
php 字符串 拼接值 +. 就可以
数组 直接[] 赋值就可以

php 正则函数 preg_match() 如果纯判断数字 is_numeric() 也可以


自己试试吧
zjsxwc
2022-10-11 17:01:46 +08:00
直接用这个 php 写的 js 解释器,在 php 里面运行 js
https://github.com/hiltonjanfield/js4php5
wuxiaoqing234
2022-10-11 17:36:50 +08:00
@zjsxwc 你搞这个还得看入参和出参, 还不如直接看语法..
ywmail
2022-10-12 08:57:39 +08:00
@Marinaaaa @iddddg @kamal
copilot 还有另一个 vs code 插件:github copilot labs ,用它转的。

@landers2015
折腾了一圈替代品,发现和 copilot 差距太大,最后交了$买的。
Chaningnt
2022-10-12 16:00:00 +08:00
测试了两三条数据
function convert($input){
$pdst = trim(strtoupper($input));
$isg = '';
$pit = [];
while (preg_match('/[0-9A-Z]+/', $pdst, $data) != false){
if(count($data) > 0){
$pd = $data[0];
$pdt = strlen($pd);
if(preg_match('/[0-9]+/', $pd) && strlen($pd) < 8 ){
$isg .= $pd;
}
if(strlen($isg) == 15){
$pd = $isg;
}
if(strlen($isg) >= 15){
$isg = '';
}
if(preg_match('/[0-9]+/', $pd) && strlen($pd) == 11 && substr($pd, 0, 1) == 's'){
$pit[] = substr(trim($pd), 1, 11);
}
if(preg_match('/[0-9]+/', $pd) && strlen($pd) == 13 && substr($pd, 0, 1) == 's'){
$pit[] = substr(trim($pd), 1, 13);
}
if((preg_match('/[0-9]+/', $pd) && strlen($pd) == 15) || strlen($pd) == 12 || strlen($pd) == 10 || (preg_match('/[0-9]+/', $pd) && strlen($pd) == 8)){
$pit[] = $pd;
}
$pdst = trim(substr($pdst, $pdt));
}
}
return $pit;
}
Chaningnt
2022-10-12 16:09:27 +08:00
@Chaningnt 返回结果应该是要去重,改成 return array_unique($pit);
Chaningnt
2022-10-12 16:26:50 +08:00
按 js 的格式处理了一下

function convert($input){
$pdst = trim(strtoupper($input));
$isg = '';
$pit = [];
while (preg_match('/[0-9A-Z]+/', $pdst, $data) != false){
if(count($data) > 0){
$pd = $data[0];
$pdt = strlen($pd);
if(preg_match('/[0-9]+/', $pd) && strlen($pd) < 8 ) $isg .= $pd;
if(strlen($isg) == 15) $pd = $isg;
if(strlen($isg) >= 15) $isg = '';
if(preg_match('/[0-9]+/', $pd) && strlen($pd) == 11 && substr($pd, 0, 1) == 's') $pit[] = substr(trim($pd), 1, 11);
if(preg_match('/[0-9]+/', $pd) && strlen($pd) == 13 && substr($pd, 0, 1) == 's') $pit[] = substr(trim($pd), 1, 13);
if((preg_match('/[0-9]+/', $pd) && strlen($pd) == 15) || strlen($pd) == 12 || strlen($pd) == 10 || (preg_match('/[0-9]+/', $pd) && strlen($pd) == 8)) $pit[] = $pd;
$pdst = trim(substr($pdst, $pdt));
}
}
return array_unique($pit);
}
edis0n0
2022-10-12 17:16:29 +08:00
@Chaningnt #31 谢谢!晚上测试一下

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/885938

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX