Well, get rid of the quotes, then trim, then put the quotes back.
Let's make a clean function for that :
<?php
function clean_string($string, $sep='"')
{
// check if there is a quote et get rid of them
$str = preg_split('/^'.$sep.'|'.$sep.'$/', $string);
$ret = "";
foreach ($str as $s)
if ($s)
$ret .= trim($s); // triming the right part
else
$ret .= $sep; // putting back the sep if there is any
return $ret;
}
$string = '" this is a test "';
$string1 = '" this is a test ';
$string2 = ' this is a test "';
$string3 = ' this is a test ';
$string4 = ' "this is a test" ';
echo clean_string($string)."\n";
echo clean_string($string1)."\n";
echo clean_string($string2)."\n";
echo clean_string($string3)."\n";
echo clean_string($string4)."\n";
?>
Ouputs :
"this is a test"
"this is a test
this is a test"
this is a test
"this is a test"
This handle strings with no quote, with one quote only at the beginning / end, and fully quoted. If you decide to take " ' " as a separator, you can just pass it as a parameter.