Enjoy...might be some better way than that given here.....
<?
$input = 'w1 w2 "w3 w4" w5 w6 w7 "w8 w9" w10 "w11 w12"';
echo "Input String -> ".$input;
echo "<br>";
// find the total occurrences of "
for ($i=0;$i<strlen($input);$i++){
if (substr($input,$i,1) == '"'){
$cnt++;
}
}
// explode the string with separator "
$arr=explode('"',$input);
$j=1;
// replace the space with underscore at corresponding cells in array
while ($j <= $cnt){
$arr[$j]='"'.str_replace(' ','_',$arr[$j]).'"';
$j += 2;
}
// form the output string
for ($k=0;$k<sizeof($arr);$k++){
$output .= $arr[$k];
}
echo "Output String -> ".$output."\n";
?>