【问题标题】:split current url after "=" character with php [duplicate]用php在“=”字符后拆分当前网址[重复]
【发布时间】:2013-10-28 17:51:20
【问题描述】:

我需要有关此代码的帮助。

只需要使用 php 获取当前 url 的最后一部分。

示例:http://www.site.com/index.php?user=username bla bla and the rest words

获取 = 字符后的所有 url,我只需要 username bla bla 和其余单词

对于 javascript:document.URL.split("=")[1];

我需要php,谢谢。

我已尝试使用此代码但无法正常工作,

basename($_SERVER['REQUEST_URI']);

【问题讨论】:

  • $_SERVER['QUERY_STRING'] 可能就是您要找的。​​span>
  • 也没有必要拆分它,因为使用$_GET 似乎适合当前的url/页面。

标签: php url split


【解决方案1】:

你可以使用explode("=",$url)——这将返回一个数组,字符串用“=”分割

你可以使用 $_GET['user']

【讨论】:

  • 感谢朋友们! $_GET['user'] 完美!
  • 没问题...你能把它标记为接受吗?!谢谢!!
【解决方案2】:

使用$_SERVER['QUERY_STRING']获取查询字符串,然后使用explode提取=之后的数据。

例如:使用网址http://www.site.com/index.php?user=username

<?php

$text = explode("=", $_SERVER['QUERY_STRING']);
print_r($text);
?>

输出:

Array
(
    [0] => "http://www.site.com/index.php?user"
    [1] => "username"
)

【讨论】:

    【解决方案3】:

    您似乎想要通过 GET 方法传递的值 你可以像这样简单地获得它的价值

    <?php
    if (isset($_GET['user']))
        $a=$_GET['user'];
    ?>
    

    注意:仅当您确定 ?' and before=` 之后的变量是用户时才使用此变量,否则您也可以使用

    <?php
    if (!empty($_GET)) {
        $a=explode("=",$_SERVER["QUERY_STRING"]);
        echo $a[1];}
    else
        echo 'Please provide username';
    ?>
    

    ?>

    【讨论】:

      【解决方案4】:
      $current_url =  $_SERVER["QUERY_STRING"]; 
      $a=explode("=",$current_url);
          echo $a[1];
      

      //CI 框架的 OR

      $current_url =  current_url();
      $a=explode("=",$current_url);
       echo $a[1];
      

      【讨论】:

        猜你喜欢
        • 2017-07-22
        • 2011-07-06
        • 1970-01-01
        • 2010-09-22
        • 2017-08-04
        • 2018-12-28
        • 1970-01-01
        • 1970-01-01
        • 2017-12-24
        相关资源
        最近更新 更多