PHP 是最好的语言, 但是……


php-mysql PHP 是最好的语言, 但是...... I.T. PHP是最好的语言 程序设计

php-mysql

很久之前, 我就说了, PHP是最好的语言, 是宇宙中最好的语言, 因为方便啊, PHP有几千个估计得上万个函数了: http://php.net/manual/en/indexes.functions.php

所以, 生活工作上, 我也会拿PHP来写些命令行的脚本, 比如这个

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
if ($argc < 2) {
    die();
}
if (!is_file($argv[1])) {
    die();
}
$offset = $argv[2] ?? 0;
 
// Search and Replace using Regular Expression with Arithmetic Evaluation
echo preg_replace_callback(
        '~\<z\>(.*)\</z\>~i', // pattern for Z coordinate
        function ($matches) {
            global $offset;
            return "<z>".($matches[1] + $offset)."</z>";
        },
        file_get_contents($argv[1])
);
<?php
if ($argc < 2) {
    die();
}
if (!is_file($argv[1])) {
    die();
}
$offset = $argv[2] ?? 0;
 
// Search and Replace using Regular Expression with Arithmetic Evaluation
echo preg_replace_callback(
        '~\<z\>(.*)\</z\>~i', // pattern for Z coordinate
        function ($matches) {
            global $offset;
            return "<z>".($matches[1] + $offset)."</z>";
        },
        file_get_contents($argv[1])
);

目的是用来把一个XML中 所有 <Z></Z> 标签的数值给改变一个偏移. 命令行里这么用:

php shift.php data.xml 1.0 > new_data.xml

我自己平时需要当然觉得超级好用, 碰巧今天公司的一个 support engineer 在客户的机器上也需要做这一件事情 – 因为软件还不支持这个功能, 只能暂时这样帮助客户来实现 work-around.

这个 support engineer 不知道啥是PHP, 也无法在客户的机器上安装程序(但可以下载PHP移动版). 想来想去, 为了运行上面的PHP脚本特意去下载一大包PHP运行库也不是很友好, 于是只能把上面的脚本给翻译成 WINDOWS 支持的VBS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Option Explicit 
 
Dim args
Set args = Wscript.Arguments
 
If args.Count < 2 Then
    WScript.Echo "First Parameter: BSM Path. Second Parameter: Z Offset."
    WScript.Quit
End If 
 
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject") 
 
Dim bsm: bsm = args(0)
Dim offset: offset = args(1)
 
If Not IsNumeric(offset) Then
    WScript.Echo "Second Parameter: Not a Number"
    WScript.Quit
End If 
 
offset = CDbl(offset)
 
If Not fs.FileExists(bsm) Then
    WScript.Echo "File Not Found: " & bsm
    WScript.Quit    
End If
 
Dim text
Set text = fs.OpenTextFile(bsm, 1)
 
Dim objRegEx
Set objRegEx = CreateObject("VBScript.RegExp")
 
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = "<Z\>(.*)\</Z\>"
 
Dim s, matches
Do Until text.AtEndOfStream
    s = text.ReadLine
    Set matches = objRegEx.Execute(s)
    If matches.Count > 0 Then
        WScript.Echo "<Z>" & matches.Item(0).SubMatches.Item(0) + offset & "</Z>"
    Else
        WScript.Echo s
    End If
Loop
 
text.Close
Set text = Nothing
Set fs = Nothing
Set args = Nothing
Set objRegEx = Nothing
Option Explicit 

Dim args
Set args = Wscript.Arguments

If args.Count < 2 Then
    WScript.Echo "First Parameter: BSM Path. Second Parameter: Z Offset."
    WScript.Quit
End If 

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject") 

Dim bsm: bsm = args(0)
Dim offset: offset = args(1)

If Not IsNumeric(offset) Then
    WScript.Echo "Second Parameter: Not a Number"
    WScript.Quit
End If 

offset = CDbl(offset)

If Not fs.FileExists(bsm) Then
    WScript.Echo "File Not Found: " & bsm
    WScript.Quit    
End If

Dim text
Set text = fs.OpenTextFile(bsm, 1)

Dim objRegEx
Set objRegEx = CreateObject("VBScript.RegExp")

objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = "<Z\>(.*)\</Z\>"

Dim s, matches
Do Until text.AtEndOfStream
    s = text.ReadLine
    Set matches = objRegEx.Execute(s)
    If matches.Count > 0 Then
        WScript.Echo "<Z>" & matches.Item(0).SubMatches.Item(0) + offset & "</Z>"
    Else
        WScript.Echo s
    End If
Loop

text.Close
Set text = Nothing
Set fs = Nothing
Set args = Nothing
Set objRegEx = Nothing

看吧, PHP几行就能安排的明明白白的事情在VBS上就得啰哩啰嗦的写个几十行. 不过VBScript的好处就是所有WINDOWS版本都支持, 发过去, 客户只需要按照说明在命令行里敲入命令即可跑VBS了.

GD Star Rating
loading...
本文一共 305 个汉字, 你数一下对不对.
PHP 是最好的语言, 但是……. (AMP 移动加速版本)
上一篇: 想知道CHROME到底有多占内存么? WINDOWS下批处理查看程序内存用量
下一篇: 编程练习题 - 如何合并两个有序的数组?

扫描二维码,分享本文到微信朋友圈
0fe0054582ec146e8ceb3534b22e37cd PHP 是最好的语言, 但是...... I.T. PHP是最好的语言 程序设计

2 条评论

评论