我对PHP很新,所以请不要期望我知道高级技术...
我目前有
$cat = "A Phrase Here"
在哪里导致
A Phrase Here
和以下代码:
$cat = "'$cat'";
echo $cat;
因为我希望它的最终结果是:
'A Phrase Here'
但是,出现的是:
' A Phrase Here '
我如何在" a"one_answers"这里"之后摆脱额外的空间?
谢谢。
编辑
看来原始的$cat
似乎有问题的空间,并且需要trim
进行修复。我对所有人的误解感到抱歉。
$cat = "'$cat'"
工作完全很好。
如果您看到额外的空间,则意味着原始字符串包含它们。您可以用饰边将它们删除。
您不需要第二个分配,所以:
$cat = "A Phrase Here";
echo $cat; // this is enough
$cat = "A Phrase here";
$cat = $cat . ", and this should be an extra string";
echo $cat;
$cat = " this string with many white spaces ";
echo trim($cat); // will trim the white spaces before and after the string;
您已经有额外的空间,而不是分配,而不是echo
行。为了确保不会通过额外的空间,您可以使用trim
:
$cat = "'".trim($cat)."'";
,最好查看您的代码并找到添加空间的位置。(或仅var_dump
一切)
echo
不是唯一的选择。使用printf()
:
$cat = "A Phrase Here"
printf("'%s'", trim($cat) );
避免'
划界字符串中可变替换的问题。
编辑:添加了trim()
:http://php.net/trim