hillz hillz - 1 month ago 4
PHP Question

What is the equivalent "grep" command in php?

Please bear with me since I'm still really new in PHP. So I have a

config
file like this:

profile 'axisssh2'
server '110.251.223.161'
source_update 'http://myweb.com:81/profile'
file_config 'udp.group-1194-exp11nov.ovpn'
use_config 'yes'
ssh_account 'sgdo.ssh'


I want to create a PHP variable named
$currentprofile
with the value of
axisssh2
, the value keeps changing. With
grep
in bash I can just do

currentprofile=$(cat config | grep ^profile | awk -F "'" '{print $2}')


But I have no idea how to do that with PHP. Please kindy help me how to do that, thank you.

UPDATE:
So I tried
preg_match
like this but it only shows the value of
1


$config=file_get_contents('/root/config');
$currentprofile=preg_match('/^profile /', $config);
echo "Current Profile: ".$currentprofile;


Please tell me what's wrong.

Answer

I'm going out on a limb to answer a question that you didn't ask:

$array = parse_ini_string(str_replace(' ', '=', file_get_contents($file)));
print_r($array);

Yields:

Array
(
    [profile] => axisssh2
    [server] => 110.251.223.161
    [source_update] => http://myweb.com:81/profile
    [file_config] => udp.group-1194-exp11nov.ovpn
    [use_config] => yes
    [ssh_account] => sgdo.ssh
)

So just:

echo $array['profile'];