<?php

$str = 'Hello world //this is a comment
Testing //not testing
Test again';

echo preg_replace('~//.*$~m', 'foo \\0 bar', $str);