简体   繁体   中英

PHP Search for text in a file write to a location in that file, alphabetically

I'm trying to write a, fixed size, block of text in to a list of other fixed size blocks of text alphabetically.

To give you and idea of what it looks like:

;Config generated on 2012-11-12 14:03:59


;Header1

exten => 01243573056,1,Answer()

exten => 01243573056,n,ResetCDR()

exten => 01243573056,n,Set(CDR(accountcode)=21)

exten => 01243573056,n,Set(CDR(userfield)=inbound)

exten => 01243573056,n,Set(MONITOR_FILENAME=Header1_${UNIQUEID})

exten => 01243573056,n,Set(CALLERID(name)=Header1)

exten => 01243573056,n,Queue(Header1)

exten => 01243573056,n,Hangup()

;Header2

exten => 01243573057,1,Goto(IVR,a,1)

exten => 01243573057,50,ResetCDR()

exten => 01243573057,n,Set(CDR(accountcode)=118)

exten => 01243573057,n,Set(CDR(userfield)=inbound)

exten => 01243573057,n,Set(MONITOR_FILENAME=Header2_${UNIQUEID})

exten => 01243573057,n,Set(CALLERID(name)=Header2)

exten => 01243573057,n,Queue(Header2)

exten => 01243573057,n,Hangup()

Apologies about the huge quote,

So they all conform to the same template (edit, they mostly do, the Header2 block is a bad example) after the headers but there is the flag at the top and odds and ends at the end of the text file which need to be there for the program that they config.

However using this example I need to find where Header1's block ends and Header2's block begins and then insert Header3's block after Header1's block so that they end up being alphabetical.

I was thinking of using fseek() for this but it doesn't search by text from what I can see.

Any help would be appreciated.

The file you have pasted has not registers with fixed-size.

for example:

exten => 01243573056,n,Set(CDR(accountcode)=21)   // in Header1

exten => 01243573057,n,Set(CDR(accountcode)=118)  // in Header2: it has 1 byte more

You have to pass a number of bytes to fseek function in order to avoid the read on them. Using this structure, you have to read along the file to search for ;Header* X * till you find it.

If you use padding to have a fixed-size, then you can use n * sizeof(register) when fseeking.

You can read it line by line, if line start with "[" record line position in hash for example.

That way it organized in freepbx.org core, you can read it to got more ideas about parsing.

But correct solution for asterisk is use gosub or macro, store params only and not write again and again same dialplan.

You could write a simple Tokenizer/Parser and read each char or parse lines with file .

$lines = file('extensions.conf');

foreach ($lines as $num => $line) {
    // simple parse with compare or strpos
    if (trim($line) == ';Header2') {
        echo "Match at Line #{$num}: " . $line . "\n";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM