Check out preg_replace here, this is what you are lookin for.
preg_replace
// From the documentation.preg_replace($regularExpression, $replacement, $subject);
What you're looking for is preg_replace:
$pg = preg_replace('{regex goes here}', '{replacement goes here}', $pg);