K.D
K.D

Reputation: 21

Reading txt file with php

I'm trying to pars ip in line which located 2 lines upper from specific words that I know, can somebody give an idea how to do it? This is part of my code...

if (($handle = fopen('FW1-20160802_1530.conf', "r")) !== FALSE){
while (($data = fgetcsv($handle, 1000,";")) !== FALSE) 
..
else if (preg_match('/(.*site-5552)/',$data[0],$match))
            {
                list(,$line)=$match;
                $REV_ARRAY[$site_name]['line']=$line;           
            }

That gives me the third line of the following output:

edit 2
        set dst 10.23.255.252 255.255.255.252
        set device "abc"
        set comment "Static Route site-5552"

But as I mentioned above I need to catch the ip: "10.23.255.252" which exists two lines upper.

The text file is too big so I pasted just part of it. According to site number (in this example- 5552) I need to find the relevant ip (10.93.255.252)

edit "Site-5552"
    set asdaa
    set asdsdaa
    set aadsda
    set aasdda
    set aaaa
    set adsaa
    set aadsa
    set vvxv
    set dsds
    set czx

ssasd

edit 2
        set dst 10.93.255.252 255.255.255.252
        set device "sdasdsad"
        set comment "Static Route for LTE site-5552"

Upvotes: 0

Views: 72

Answers (1)

BeetleJuice
BeetleJuice

Reputation: 40896

K.D you need the s modifier to tell the regex engine to include new-line characters when matching against .

Change your regex pattern from:

/(.*site-5552)/

To:

/set dst ([\d\.]+).*site-5552/s

Live demo

Upvotes: 1

Related Questions