Asked  7 Months ago    Answers:  5   Viewed   20 times

I need to parse the current url so that, in either of these cases:

I can get the return value of "abc" (or whatever text is in that position). How can I do that?



You can use parse_url();

$url = '';


echo parse_url($url, PHP_URL_PATH);

which would give you

    [scheme] => http
    [host] =>
    [path] => /abc/

Update: to get current page url and then parse it:

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
 } else {
 return $pageURL;


echo parse_url($url, PHP_URL_PATH);

source for curPageURL function

Wednesday, March 31, 2021
answered 7 Months ago
$query = $_SERVER['PHP_SELF'];
$path = pathinfo( $query );
$what_you_want = $path['basename'];


Wednesday, March 31, 2021
answered 7 Months ago

A tokenizer is not the same as a parser. The tokenizer just produces tokens and not in a tree format. For those who are actually looking to produce an AST for PHP similar to what ruby_parser does for Ruby, use the PHP-Parser project (

The PHP-Parser project also comes with a pretty printer that turns your AST back to PHP.

Wednesday, March 31, 2021
answered 7 Months ago

You should be able to do


NOTE: http_build_url is only available by installing pecl_http.

According to the docs it's designed specifically to handle the output from parse_url. Both functions handle anchors, query params, etc so there are no "other properties not mentioned on the $url".

To add http:// when it's missing, use a basic check before parsing it:

if (strpos($url, "http://") != 0)
    $url = "http://$url";
Saturday, May 29, 2021
answered 5 Months ago

I am surprised this wasn't mentioned yet: what about using Zend_Reflection of Zend Framework? This may come in handy especially if you work with a software built on Zend Framework like Magento.

See the Zend Framework Manual for some code examples and the API Documentation for the available methods.

There are different ways to do this:

  • Pass a file name to Zend_Reflection_File.
  • Pass an object to Zend_Reflection_Class.
  • Pass an object and a method name to Zend_Reflection_Method.
  • If you really only have the comment string at hand, you even could throw together the code for a small dummy class, save it to a temporary file and pass that file to Zend_Reflection_File.

Let's go for the simple case and assume you have an existing class you want to inspect.

The code would be like this (untested, please forgive me):

$method = new Zend_Reflection_Method($class, 'yourMethod');
$docblock = $method->getDocBlock();

if ($docBlock->hasTag('return')) {
    $tagReturn = $docBlock->getTag('return'); // $tagReturn is an instance of Zend_Reflection_Docblock_Tag_Return
    echo "Returns a: " . $tagReturn->getType() . "<br>";
    echo "Comment for return type: " . $tagReturn->getDescription();
Sunday, August 1, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :