简体   繁体   中英

How to match the second occurrence in php?

I have a text file:

; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
doc_root = "/www/"                                                   
user_dir =                                 
extension_dir = "/usr/lib/php"
enable_dl = On              
;cgi.force_redirect = 1     
;cgi.nph = 1                
;cgi.redirect_status_env = ;
cgi.fix_pathinfo=1          
;fastcgi.impersonate = 1;
;fastcgi.logging = 0   
;cgi.rfc2616_headers = 0

I want to match /www/ here's what I've tried so far:

<?php
$phpini = file_get_contents('/etc/php.ini');
preg_match('/doc_root = ".*/', $phpini, $doc_root);
echo $doc_root[0];
?>

But it prints doc_root = "/www/" instead of /www/ . How can I do this ?

Not an answer to your exact question, but how about you use parse_ini_file() for files:

$phpini = parse_ini_file('/etc/php.ini');
echo $phpini['doc_root'];

Or for the currently loaded php.ini :

echo get_cfg_var('doc_root');

Or the current setting (could have been modified by .htaccess etc.):

echo ini_get('doc_root');

Another option would be:

echo $_SERVER['DOCUMENT_ROOT'];

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