Well, get rid of the quotes, then trim, and then return the quotes.
Make a clean function for this:
<?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 line descriptor is without a quote, with one quote only at the beginning / end and is fully quoted. If you decide to take "" as a separator, you can simply pass it as a parameter.
source share