【问题标题】:Find text and handle values to PHP variables查找文本并处理 PHP 变量的值
【发布时间】:2017-02-07 19:36:20
【问题描述】:

我有硬核。我在 PHP 变量中有这个文本,它来自 SSH 的执行命令,我想将文本提取到变量中。

所以,这是示例文本:

  Huawei Integrated Access Software (MA5683T).
  Copyright(C) Huawei Technologies Co., Ltd. 2002-2013. All rights reserved.

  -----------------------------------------------------------------------------
  User last login information:
  -----------------------------------------------------------------------------
  Access Type : SSH 
  IP-Address  : 0.0.0.0 ssh
  Login  Time : 07.02.2017 20:01:48+01:00
  Logout Time : 07.02.2017 20:01:59+01:00
  -----------------------------------------------------------------------------

MA5683T>enable

MA5683T#display ont autofind all
   ----------------------------------------------------------------------------
   Number              : 1
   F/S/P               : 0/4/0
   Ont SN              : 48575443E1EAC883 (HWTC-E1EAC883)
   Password            : 0x00000000000000000000
   Loid                : 
   Checkcode           : 
   VendorID            : HWTC
   Ont Version         : 635.A
   Ont SoftwareVersion : V3R015C10S106
   Ont EquipmentID     : 010H
   Ont autofind time   : 06.02.2017 11:35:08+01:00
   ----------------------------------------------------------------------------
   Number              : 2
   F/S/P               : 0/4/0
   Ont SN              : 48575443ED9B1582 (HWTC-ED9B1582)
   Password            : 0x00000000000000000000
   Loid                : 
   Checkcode           : 
   VendorID            : HWTC
   Ont Version         : 635.A
   Ont SoftwareVersion : V3R015C10S106
   Ont EquipmentID     : 010H
   Ont autofind time   : 07.02.2017 15:57:35+01:00
   ----------------------------------------------------------------------------
   Number              : 3
   F/S/P               : 0/4/1
   Ont SN              : 48575443E1DA5683 (HWTC-E1DA5683)
   Password            : 0x00000000000000000000
   Loid                : 
   Checkcode           : 
   VendorID            : HWTC
   Ont Version         : 635.A
   Ont SoftwareVersion : V3R015C10S106
   Ont EquipmentID     : 010H
   Ont autofind time   : 07.02.2017 09:20:57+01:00
   ----------------------------------------------------------------------------
   The number of GPON autofind ONT is 3

MA5683T#

我需要提取“Ont SN”(例如 48575443E1EAC883)和“F”、“S”和“P”值(F/S/P : 0/4/0 in text)。

输出将是:

$serial = "48575443E1EAC883"
$f = 0
$s = 4
$p = 0

因为有3条记录,除以------行,我需要放入数组中。

谢谢你的建议。

【问题讨论】:

  • 文本提取非常简单,但使用原始字符串而不是我们在这里看到的字符串会更容易。
  • 序列号是十六进制还是字母数字?我认为F/S/P\h+:\h+(?<f>\d+)/(?<s>\d+)/(?<p>\d+)\s*Ont\h+SN\h+:\h+(?<serial>[A-F\d]+) 会这样做,然后您可以检查匹配项的索引以查看它是什么。 (也不要在此正则表达式中使用 / 分隔符,否则您需要转义。)regex101.com/r/ZUj8Jh/1
  • 序列号是 HEX。到目前为止我什么都没有。我的目标是今晚成功。
  • 那么提供的正则表达式有效吗?
  • 是的,但是目前,我只有一条来自 SSH 命令的记录,所以输出的 SN 是三倍: Array ( [0] => F/S/P : 0/4/0 Ont SN : 48575443E1EAC883 [f] => 0 [1] => 0 [s] => 4 [2] => 4 [p] => 0 [3] => 0 [串行] => 48575443E1EAC883 [4] => 48575443E1EAC883)

标签: php arrays regex string substr


【解决方案1】:

为了匹配Ont SN

/Ont SN\s*:\s*([\w]+)/g

https://regex101.com/r/Q7zuk1/1


对于匹配F/S/P

/F\/S\/P\s*:\s*(\d+)\/(\d+)\/(\d+)/g

https://regex101.com/r/Q7zuk1/2

【讨论】:

  • 好的,但我需要将它抓取到数组中,因为输出必须打印为带行的表格。列是 SN、F、S、P。
猜你喜欢
  • 2010-12-01
  • 2017-03-10
  • 2016-08-13
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2016-08-31
  • 2018-04-29
  • 1970-01-01
相关资源
最近更新 更多