简体   繁体   English

Javascript字符串/无空间的变量串联

[英]Javascript string/variable concatenation without space

while using a ajax script, I made the php part echo a value then in the jQuery part I alert it. 在使用ajax脚本时,我使php部分回显了一个值,然后在jQuery部分中提醒了它。

So we have something like this: 所以我们有这样的事情:

<?php echo "1"; ?>

//Javascript
alert("the value is"+phpvalue);

The alert shows up like "the value is 1". 警报显示为“该值为1”。 Now my question is how do I remove the space between the "the value is" and +phpvalue? 现在,我的问题是如何删除“ value is”和+ phpvalue之间的空格? So it show up like "the value is1". 因此它显示为“值是1”。 I tried trim() on the php part but it doesn't change anything. 我在php部分试过trim(),但没有任何改变。

Apreciate any help and sorry if it is a noob question =/ 感谢任何帮助,如果这是一个菜鸟问题,对不起,谢谢//

In http response can be some other caracters than what you print. 在http中,响应可能是您打印的其他字符。 For example, if there is spaces before php tag .. BOM character in the begining of the php file... so if you trim just the variable, that does not detemrimate, that you get purely the variable on client side without junk characters. 例如,如果php标记的开头是php标记.. BOM字符之前的空格...因此,如果仅修剪变量,则不会确定,您可以在客户端获得纯变量而没有垃圾字符。

So you should use trim on client side in javascript: For example jQuery trim ( http://api.jquery.com/jQuery.trim/ ) 因此,您应该在javascript中的客户端上使用trim:例如jQuery trim( http://api.jquery.com/jQuery.trim/

   alert("the value is"+$.trim(phpvalue));

Or use nativ js trim function from phpjsorg ( http://phpjs.org/functions/trim/ ): 或使用phpjsorg( http://phpjs.org/functions/trim/ )中的nativ js trim函数:

   alert("the value is"+trim(phpvalue));

Not sure exactly what you want but here are two situations 不确定您到底想要什么,但有两种情况

If you just are dealing with excess whitespace on the beginning or end of the string you can use trim(), ltrim() orrtrim() to remove that.

If you are dealing with extra spaces within a string consider a preg_replace of multiple whitespaces " "* with a single whitespace " "

$foo = preg_replace( '/\s+/', ' ', $foo );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM