tags:

views:

27

answers:

1

Hi,

I have a config file, in etc/ called 1.conf

Here is the contents..

[2-main]
exten => s,1,Macro(speech,"hi {$VAR1} how is your day going?")
exten => s,4,Macro(dial,2,555555555)
exten => s,2,Macro(speech,"lkqejqe;j")
exten => s,3,Macro(speech,"hi there")
exten => s,5,Macro(speech,"this is a test ")
exten => s,6,Macro(speech,"testing 2")
exten => s,7,Macro(speech,"this is a test")
exten => 7,1,Goto(2-tester2,s,1)
exten => 1,1,Goto(2-aa,s,1)
[2-tester]
[2-aa]
exten => 1,1,Goto(2-main,s,1)



How can I read the content in between speech for example..

exten => s,6,Macro(speech,"testing 2")

Just get "testing 2" from that.

Thank you in advance!

+2  A: 

If I understand correctly, this

$text = file_get_contents('/etc/1.conf');
preg_match_all('/speech,"(.*)"/', $text, $match);

will fill the $match array with all the text from speech lines. Specifically, $match[1] will be an array containing just the string inside the quotes, so you can do this for convenience and readability:

$speeches = $match[1];
echo $speeches[0]; // or 1 or 2 etc.
kemp
Nice thank you!How about if its under [2-aa] instead of [2-main]? How would you do that?
zx
That regexp matches every line containing `speech` followed by a comma and a string enclosed in double quotes, wherever it appears in the file.
kemp
Ahh I see.So what that does it prints out "speech,"testing 2"" for me.How would I just print out what is inbetween the "" which is testing 2?
zx
The `$match[0]` array contains the full string found, while the `$match[1]` array contains only the value inside the quotes. Try `print_r($match)` to see its structure.
kemp
print $match[1] just prints out "Array" for me
zx
That's because `$match[1]` is an array itself. The values are in `$match[1][0]`, `$match[1][1]`, etc.
kemp