duanhan5230 2016-06-29 09:37
浏览 89
已采纳

在smarty中创建URL编码的字符串

I am trying to create a string of values taken from variables in the backend with the following structure:

Before encoding:

transaction_id=0815/2009;transaction_cid=54AB;item_id=402163045080;item_va lue=25.20;item_quantity=1; 
transaction_id=0815/2009;transaction_cid=54AB;item_id=402163045080;item_va lue=25.20;item_quantity=1;

After encoding:

transaction_id%3D0815%2F2009%3Btransaction_cid%3D54AB%3Bitem_id%3D40216304 5080%3Bitem_value%3D25.20%3Bitem_quantity%3D1%3Bitem_id%3D847163029054%3Bi tem_value%3D16.81%3Bitem_quantity%3D2

I have managed to create an array with the necessary data in this form:

'[{"transaction_id":"233684","transaction_cid":"d2871c13c507583048d8ecf4a16f94c0","i tem_id":"3524","item_value":"4915.13","item_quantity":"1"}]',

But what I need is all these elements of the array in a url encoded string.

I am out of ideas since all that I try seems to not work.

Using JSON.stringify keeps the ":" and the """, using alert() or join also keeps the ":" and is not performant.

Example array:

arr : {key1: 'a', key2:'b', key3:'c'}

non encoded result:

str : 'key1=a;key2=b;key3=c'

desired result:

str : 'key1%3Da%3Bkey2%3Db%3Bkey3%3Dc'

Here is my code so far:

[{foreach from=$orderArticles item="currOrderArticle"}]
        [{assign var="currBasePrice2" value=$currOrderArticle->getBasePrice()}]

    products_info.push(
              {
              transaction_id: '[{$order->oxorder__oxordernr->value}]', 
              transaction_cid: '[{$order->oxorder__oxuserid->value}]', 
              item_id: '[{$currOrderArticle->oxorderarticles__oxartnum->value}]',
              item_value: '[{$basket->getDiscountedNettoPrice()}]',
              item_quantity: '[{$currOrderArticle->oxorderarticles__oxamount->value}]'
              });

    [{/foreach}]

Any ideas on how this can be accomplished?

  • 写回答

1条回答 默认 最新

  • douxian4888 2016-06-29 10:21
    关注

    You can combine json_encode (or serialize if you only need to use it in php) and escape:

    {$arr|json_encode|escape:'url'}
    

    Also, if you want to make the string shorter you can use compression:

    {$arr|json_encode|gzcompress|base64_encode|escape:'html'}
    

    Though that may be a bit overkill for short arrays and you'll have to base64_decode, gzuncompress and json_decode the string when you receive it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥115 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line