Ну, избавьтесь от кавычек, затем обрежьте, затем верните кавычки обратно.
Давайте сделаем чистую функцию для этого:
<?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"
Этот дескриптор строк без кавычек, с одной кавычкой только в начале / конце и полностью кавычками Если вы решили использовать «» в качестве разделителя, вы можете просто передать его в качестве параметра.