【问题标题】:get the parameter from the url which has slace in codeigniter从在codeigniter中有空格的url中获取参数
【发布时间】:2018-05-02 11:49:09
【问题描述】:

我的网址如下所示

http://localhost/demo/company/warehouse_subportal/printEway/CN/00003

我的发票编号是 CN/00003,有时是 CN-00003。 我想要的是从 URL 获取发票。现在我只得到 CN。我知道那是因为 /。我怎样才能得到像CN / 00003这样的发票?请帮助我提前谢谢。

【问题讨论】:

  • 网址编码你的网址:/printEway/CN%2F00003
  • 请您分享任何示例。我会遵循。谢谢

标签: php codeigniter url


【解决方案1】:

您可以在生成链接或网址示例时使用urlencode

<?php
echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>

然后使用urldecode解码发票值,例如:

<?php
$query = "my=apples&are=green+and+red";

foreach (explode('&', $query) as $chunk) {
    $param = explode("=", $chunk);

    if ($param) {
        printf("Value for parameter \"%s\" is \"%s\"<br/>\n", urldecode($param[0]), urldecode($param[1]));
    }
}
?>

注意,只有发票部分需要编码或解码,部分

CN/00003

另一种可能的解决方案是编码成base64,使用base64_encode,例如:

<?php
$str = 'This is an encoded string';
echo base64_encode($str);
?>

并使用base64_decode对其进行解码,例如:

<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>

【讨论】:

  • 感谢大家。现在 base64_encode() 和 base64_decode() 解决了我的问题,谢谢
【解决方案2】:
<?php

$data='http://localhost/demo/company/warehouse_subportal/printEway/CN-00003';

$data=explode('/',$data);

if(is_numeric(array_values(array_slice($data, -1))[0])){
    $invoice=implode(array_slice($data, -2, 2, true),'/');

}
else{
    $invoice=array_values(array_slice($data, -1))[0];
}

echo $invoice;

所以基本上我会检查 url 的最后一个字段,看看它是否只包含数字或还有字母,并根据响应我回显正确的值。

适用于 / 和 -

【讨论】:

  • URL 也可以是问题中提到的/CN-00003,在这种情况下这不起作用:)
  • 例如,如果我的发票没有这样的 CN-00003 它将如何工作
  • 有时我的发票没有像这样的特殊字符 10001001
  • 它有什么?那么最后一个字段可以是什么? 10001001 这些是数字而不是特殊字符。
【解决方案3】:

您可以使用分段。

if(strpos($this->uri->segment(4), '-') !== FALSE){
   $invoice_num = $this->uri->segment(4);
}
else {
   $invoice_num = $this->uri->segment(4).'/'.$this->uri->segment(5);
}

https://www.codeigniter.com/user_guide/libraries/uri.html#CI_URI::segment

【讨论】:

  • 如果发票没有斜线或 iphon
【解决方案4】:

对参数进行编码和解码将解决您的问题。

示例:

准备网址时:

$invoice_number = urlencode("CN/00003") // Or "CN-00003"

$url = "http://localhost/demo/company/warehouse_subportal/printEway/" . $invoice_number

从 url 获取发票号码:

$invoice_number = urldecode($this->uri->segment(4));

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多