【问题标题】:PHP preg_split Apache & Powershell equivalentPHP preg_split Apache 和 Powershell 等效
【发布时间】:2012-09-18 15:26:02
【问题描述】:

我在建设什么:

脚本使用 GIT-POSH 归档工作目录,并使用 Windows Powershell SSH 模块将其发送到 Linux Apache 服务器。

为方便起见,脚本还需要报告本地机器和 Apache 服务器上的现有文件。


问题:

这是我需要字符串操作来分隔命名约定中的每个块的地方。

我打算使用下划线block_block,直到我意识到某些块已经包含下划线。 这是我决定用括号封装每个块[block][block]

在 PHP 中,我会使用 preg_split 将每个部分拉出到一个关联数组中。

[product][branch_name][date][time][commit_hash]


预期用途:

--> Get-Product $string
--> product123
--> Get-Branch $string
--> branch123


我的问题:

  • 我如何preg_split 这个字符串使用相同的方式使用 powershell 和 apache?
  • 支持相同操作的更好命名约定?

【问题讨论】:

  • 你能提供更多关于输入和预期输出的信息吗?
  • @MikeShepard 请查看编辑。
  • 您可以使用 C# 正则表达式库,恕我直言,这是我们所做的,以获得 preg_split 行为。查看在 powershell 中加载/使用程序集。
  • 我看到了编辑,但你没有说输入是什么或 $string 是什么。

标签: php windows linux apache powershell


【解决方案1】:

PHP 的preg_split 可以用System.RegularExpressions.Split() 来模仿。有一些注意事项,即Split() 将返回空字符串来替换它所替换的标记,因此需要进行一些过滤。像这样,

$data = "[product][branch_name][date][time][commit_hash]"
$arr = [Regex]::Split($data, "[\[\]]") | ? { $_.length -gt 0 }
$arr

输出:

product
branch_name
date
time
commit_hash

如果没有过滤子句? { $_.length -gt 0 } - 它将排除长度为零的字符串对象 - 输出会略有不同:

product

branch_name

date

time

commit_hash

此行为在 MSDN 中为 is documented

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 2015-05-17
    • 2023-03-15
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多