php 传递带空格的字符串变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19353368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:20:56  来源:igfitidea点击:

Passing string variable with spaces

javascriptphpjquerycodeigniterpost

提问by rotaercz

In the following code:

在以下代码中:

    <script type="text/javascript">
        function updateView(set) {
            $.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
                $( "#content" ).html( data );
            });
        }
    </script>

'set' is a string variable which can have spaces in it. I'm noticing when it has spaces it's not working correctly. How can I fix this?

'set' 是一个字符串变量,其中可以有空格。我注意到当它有空格时它不能正常工作。我怎样才能解决这个问题?

EDIT: For clarity, I'd like to keep the spaces intact.

编辑:为了清楚起见,我想保持空格完整。

回答by mplungjan

Use $.trim(set)to remove leading or trailing spaces and either

使用$.trim(set)除去开头或结尾的空格,要么

set.replace(/ /g,"+")  

or

或者

encodeURI(set)

to keep the spaces inside the string
(refer When are you supposed to use escape instead of encodeURI / encodeURIComponent?)

保留字符串内的空格
(请参阅何时应该使用转义而不是 encodeURI / encodeURIComponent?

To do both in one go just chain them

一次性完成,只需将它们链接起来

$.trim(set).replace(/ /g,"+")

Note you may use %20 instead of the plus if you prefer.

请注意,如果您愿意,您可以使用 %20 而不是加号。

But is it not a parameter? If so, perhaps you want to pass it as

但它不是一个参数吗?如果是这样,也许您想将其作为

$.post("<?php echo base_url("/show_cards/load_page")."/"; ?>",{"set":$.trim(set).replace(/ /g,"+")},

回答by Rajesh Paul

You have to replace intermediate space(' ') with '%20'with replace()and eliminate boundary spaces(' ') with trim().

您必须' ''%20'with替换中间空间( ) 并用replace()消除边界空间( ' ') trim()

So use the following code-

所以使用以下代码 -

<script type="text/javascript">
    function updateView(set) {
    set=set.trim().replace(/ /g, '%20');
        $.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
            $( "#content" ).html( data );
        });
    }
</script>

回答by Rituraj ratan

use Trim

使用修剪

<script type="text/javascript">
        function updateView(set) {
          var set=$.trim(set);// by this  leading or trailing spaces removes  
            $.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
                $( "#content" ).html( data );
            });
        }
    </script>

you can also use string.replace

你也可以使用string.replace

  var set=  set.replace(/ /g,"+") ;// like that way in this all the spaces removes

回答by decongh

Rajesh Paul did a good job with his answer, i don't know why someone gave you a down vote.

Rajesh Paul 的回答做得很好,我不知道为什么有人给你投了反对票。

Good answer Rajesh, it worked for me. This is what i did: '&p_na2=' + (Txtva3).trim().replace(/ /g, '%20');

好的答案 Rajesh,它对我有用。这就是我所做的: '&p_na2=' + (Txtva3).trim().replace(/ /g, '%20');