Search code examples
phpmysqlhtmlsearchsearch-engine

Search for lines that contain X, but show more than this line when found


Straight to the point:

In

ip ssh version 2
no aaa new-model
!
dot11 ssid lala
    vlan 500
    authentication open 
    authentication key-management wpa
    wpa-psk ascii 100 40345352352352352352255325
!
!
crypto pki trustpoint TP-self-signed-3007141781

I want for instance search for a keyword 'vlan'. My code returns 'vlan 500' which is correct. And it also returns all the lines where 'vlan' is.

Now I want the search program (php) to return the whole block. In above case:

!
dot11 ssid lala
    vlan 500
    authentication open 
    authentication key-management wpa
    wpa-psk ascii 100 40345352352352352352255325
!

or without '!' ofcourse.

Any ideas for acomplishing this fast and effective?

ps: For now i'm using

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
....

and

foreach(preg_split("/(\r?\n)/", $config) as $line){
...

in it, to get the lines where the search word appears.


Solution

  • I managed to achieve that by saving indexes of block startings. I got the block start by checking the spaces infront of each line, and the last block before I found the matching word was the index where I started to display lines.