preg_replace help

Hello all,

I am need help understand this code so that I can better understand how preg_replace works…here is the code

$LOG_ROOT = preg_replace("//var/www/vhosts/(.*)/httpdocs/msi","/var/log/virtualmin/$1_",$_SERVER[“DOCUMENT_ROOT”]);
[php]

I need to get help understanding the syntax and understand what the code is doing…thanks a ton…[/php]

This command will search any instance of string described by pattern (1st argument) and replace it with another pattern (2nd argument) in the given string $_SERVER[“DOCUMENT_ROOT”]

In this example, searching pattern is: /var/www/vhosts/(.)/httpdocs
(.
) - mean any number of any characters
m, s, i after slash - are modifiers (i - forces to ignore character case, etc. you can learn about modifiers here)

The $1 expression in the replacement pattern, refers to this instance in the searching pattern: (.*)

For example, if you have:
[php]$_SERVER[“DOCUMENT_ROOT”] = ‘/var/www/vhosts/xyz/httpdocs’;[/php]

the result of such search/replace operation will be:
[php]$LOG_ROOT = ‘/var/log/virtualmin/xyz_’;[/php]

hmm…ok…what I am trying to fix is a php script that pulls the logs for analog.cfg to work with my script and echo the results…and the $LOG_ROOT is referenced in the full script with the analog commands…I think what I have incorrect is in the path information in the code snippet since on my new server my virtual /servers/domains are under /home/… not under /var/www/vhosts/…so I am not sure how to correctly change the code in the code:

[font=verdana][size=11px]$LOG_ROOT = preg_replace("//var/www/vhosts/(.*)/httpdocs/msi","/var/log/virtualmin/$1_",$_SERVER[“DOCUMENT_ROOT”]);[/size][/font][php]

Also, I currently don’t have a folder in any of my virtual domains of /httpdocs/msi…meaning I don’t have a “msi” folder…any help would be greatly appreciated…[/php]

msi - is not a folder. This is: m, s, i - modifiers for regular expressions. See link in my previous post above to learn what these modifiers means.

p.s. something is wrong with formatting in your posts. Use “php” button to markup php code, not the message text.

hmm…ok…what I am trying to fix is a php script that pulls the logs for analog.cfg to work with my script and echo the results…and the $LOG_ROOT is referenced in the full script with the analog commands…I think what I have incorrect is in the path information in the code snippet since on my new server my virtual “servers/domains” are under /home/… not under /var/www/vhosts/…so I am not sure how to correctly change the code in the code…should I change the code from:

[font=verdana][size=11px][php]$LOG_ROOT = preg_replace("//var/www/vhosts/(.)/httpdocs/msi","/var/log/virtualmin/$1_",$_SERVER[“DOCUMENT_ROOT”]);[/php][/size][/font]
[font=verdana][/size][/font]
[font=verdana][/size][size=11px]to [/size][/font]
[font=verdana][/size][size=11px][php]$LOG_ROOT = preg_replace("//home/(.
)/httpdocs/msi","/var/log/virtualmin/$1_",$_SERVER[“DOCUMENT_ROOT”]);[/php][/size][/font]
[font=verdana][/size][/font]
[font=verdana][/size][size=11px]To reflect the correct path of where my virtual servers are located since they are not located in /var/www/vhost is…thanks a ton[/size][/font]
[font=verdana][/size][/font]

I am not sure why such transformations needed to apply to the path in this script, but you have updated regular expression correctly for your paths.

Sponsor our Newsletter | Privacy Policy | Terms of Service