简体   繁体   中英

Hash replacement with regular expression not working

In this file I would like to change the UNDER_CONSTRUCTION_HASH with another hash, i do that with a regular expression.

File:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

UNDER_CONSTRUCTION_HASH=y$TwrFXK90UkJ4pq4grH1/hevPgkdOE4lEmPI9QbhVnwH1UEyYr/zA2

I would like to replace the UNDER_CONSTRUCTION_HASH with another one:

$2y$10$TwrFXK90UkJ4pq4grH1/hevPgkdOE4lEmPI9QbhVnwH1UEyYr/zA2

My code together looks like this:

$envContent = $this->filesystem->get($envPath);
$regex = '/UNDER_CONSTRUCTION_HASH=[^\s]+/';
$newLine = sprintf('UNDER_CONSTRUCTION_HASH=%s', $hash);

if (preg_match($regex, $envContent)) {
    var_dump($newLine);
    $envContent = preg_replace($regex, $newLine, $envContent);
} else {
    $envContent .= "\n".$newLine."\n";
}

The result is this:

UNDER_CONSTRUCTION_HASH=y$TwrFXK90UkJ4pq4grH1/hevPgkdOE4lEmPI9QbhVnwH1UEyYr/zA2

instead of this:

UNDER_CONSTRUCTION_HASH=$2y$10$TwrFXK90UkJ4pq4grH1/hevPgkdOE4lEmPI9QbhVnwH1UEyYr/zA2

What could be wrong here?

My package: https://github.com/larsjanssen6/underconstruction

It is a matter of escaping your dollar signs.

When $hash is injected into $newLine and used as the replacement parameter of preg_replace() , the function is assuming $2 and $1 are capture group references. You will need to park slashes in front of them to have them treated literally.

Here's a re-write:

Code: ( Demo )

$envContent='MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

UNDER_CONSTRUCTION_HASH=y$TwrFXK90UkJ4pq4grH1/hevPgkdOE4lEmPI9QbhVnwH1UEyYr/zA2';

$hash = '$2y$10$TwrFXK90UkJ4pq4grH1/hevPgkdOE4lEmPI9QbhVnwH1UEyYr/zA2';
$pattern = '/UNDER_CONSTRUCTION_HASH=\S+/';
$replace = "UNDER_CONSTRUCTION_HASH=".str_replace('$','\$',$hash);  // <-- escape your dollar signs
$newLine = "UNDER_CONSTRUCTION_HASH=$hash";

$envContent = preg_replace($pattern, $replace, $envContent, -1, $count);
if(!$count){
    //echo "no replacement made\n";
    $envContent .= "\n".$newLine."\n";
}

echo $envContent;

Output:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

UNDER_CONSTRUCTION_HASH=$2y$10$TwrFXK90UkJ4pq4grH1/hevPgkdOE4lEmPI9QbhVnwH1UEyYr/zA2

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