phparraysregexstringsubstr

Find text and handle values to PHP variables


I have hard nut. I have this text in PHP variable, which is from SSH's executed command, and I want to extract text to variables.

So, this is example text:

  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#

I need to extract "Ont SN" (for example 48575443E1EAC883) and "F", "S" and "P" values (F/S/P : 0/4/0 in text).

Output will be:

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

Because there are 3 records, divided by ------ lines, I need to put in array.

Thank you for advice.


Solution

  • For matching Ont SN:

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

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


    For matching F/S/P:

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

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