Try
$out = preg_replace("~href=['\"]*([^s'\">]+)['\"]*~i/e", "href=\"toto.php?url=".rawurlencode('\\1'), $out);
or
$out = preg_replace("~href=['\"]*([^s'\">]+)['\"]*~i/e", "href=\"toto.php?url=".rawurlencode($1), $out);
from the [MAN]preg_replace[/MAN], the /e tells the compiler to treat the replacement as php code...i.e., it tell the compiler to do the rawurlencode for ya.