How to stop sed from breaking when in combination with apg?

I am writing a script that will install WordPress automatically for me, to make my life easier as I have to deploy WordPress installs constantly.

However, I am trying to make it so I never have to interact with it, and that is taking my sleep away.

To generate the passwords for databases and whatnot, I use apg, and then use sed to replace a string with the correct password. It looks something like this:

DOMAIN="$  2" DB_NAME_USER="$  (echo $  DOMAIN | sed -e 's/\.//g')" DB_PASSWORD="$  (apg -a 1 -n 1 -m 64 -d -c cl_seed -E \_\"\'\`\/\\=)"  sed -i "s/database_name_here/$  DB_NAME_USER/g" /srv/"$  DOMAIN"/wp-config.php sed -i "s/username_here/$  DB_NAME_USER/g" /srv/"$  DOMAIN"/wp-config.php sed -i "s/password_here/$  DB_PASSWORD/g" /srv/"$  DOMAIN"/wp-config.php 

Replacing the database and username works just fine, but when replacing the password, six out of ten times the replace goes bad and I end up with something like this:

define(‘DB_PASSWORD’, ‘Ymb{X-dT~l5Agg9q561A^?npassword_hereD;$ U,s>mu*:f7|BK4?oM#OH>cHgsI9GndW@~SkBQ’);

Emphasis on the issue. Instead of removing the “password_here” part and replacing it with the actual password, it kind fuses the string in the middle of the password.

I thought it could be a character generated from apg that was messing with sed, but I removed quite a few from the password generated using -E and still get this a lot.

Same happens with other replaces, always involved apg and sed. Replacing things such as _PASSWORD_HERE_ result in the same issue from time to time.

Is there any way around this or do I have to keep trying to find characters that cause issues and then add them to apg’s exclusion list? Or maybe tell sed to replace the whole line instead of just the string I want?

Thanks in advance!