I'm working on a PHP project and regular expressions aren't my strongest skill.
I'm reading the contents of a file into a string, but need to extract a number of parameters from the string. Can anyone help me with a regular expression that can extract the value from the following definition, given I need to search by 'DB_USER':
define('DB_USER', 'admin');
So basically I need the value 'admin' as a variable...
Thanks in advance...
if( preg_match( '@.*\'DB_USER\'\s*\,\s*\'(.+)\'.*@U', $content, $matches ) ) {
echo 'DB_USER: ' . $matches[1];
}
else {
echo 'DB_USER was not found';
}
$startsAt = strpos($out, "define('DB_USER', '") + strlen("define('DB_USER', '");
$endsAt = strpos($out, "');", $startsAt);
$result = substr($out, $startsAt, $endsAt - $startsAt);
I tested this, get admin :
<?php
$strContent = "<?php
define('DB_USER', 'admin');
\$varname = 'special item';
";
//
if(preg_match("/DB_USER\W*([\w]*)/", $strContent, $matches))
{
$strUser = $matches[1];
}
else
{
$strUser = "";
}
//
echo "<textarea style=\"width: 700px; height: 90px;\">"
. "user=" . $strUser
. "</textarea>";
?>
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.