-3

i need help with this code.

need get only last part of the current url with php.

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

get all url after = character, i need only username bla bla and the rest words

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

i need for php please, thanks.

i have tried with this code but not works,

basename($_SERVER['REQUEST_URI']);
user2893064
  • 11
  • 1
  • 1

4 Answers4

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

//OR for CI framework

$current_url =  current_url();
$a=explode("=",$current_url);
 echo $a[1];
Abhijit
  • 931
  • 1
  • 9
  • 21
0

You can use explode("=",$url) -- this will return you an array with the string split with "="

you can use $_GET['user']

Andy Khatter
  • 165
  • 1
  • 9
0

Use $_SERVER['QUERY_STRING'] to get the query string and then use explode to extract data after =.

For eg: Using url http://www.site.com/index.php?user=username

<?php

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

Output:

Array
(
    [0] => "http://www.site.com/index.php?user"
    [1] => "username"
)
Joke_Sense10
  • 5,341
  • 2
  • 18
  • 22
0

it seems you want the Value Passed through GET method you can simply get its value like this

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

Note: Use this only if you are sure that after ?' and before=` the variable is user else you can also use

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

?>