English 中文(简体)
要求 uri php:达到第一级
原标题:request uri php: get first level
  • 时间:2012-05-18 23:16:41
  •  标签:
  • php

如果我有一张午餐,例如www.example.com/test/example/product.html

我如何能够只得到测试部分(最高层)

我的理解是,你将使用<代码>。 SERVER[RequestST_URI] and maybe substr or trim

但是,我对如何做到这一点感到沮丧,感谢你!

最佳回答

将该插图编成一个阵列,编号<>explode,然后由您参加。

$whatINeed = explode( / , $_SERVER[ REQUEST_URI ]);
$whatINeed = $whatINeed[1];

如果你使用PHP 5.4,你可以填写$whatINeed = 爆炸(/,_SERVER[RequestST_URI]?

问题回答
<?php
$url =  http://username:[email protected]/test/example/product.html?arg=value#anchor ;

print_r(parse_url($url));

$urlArray = parse_url($url);

/* Output:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /test/example/product.html
    [query] => arg=value
    [fragment] => anchor
)


*/

echo dirname($urlArray[path]);

/* Output:

/test    

*/

进入任何道路第一部分的最简单途径是使用<代码>strtok():

echo strtok($path,  / );

无论它是否拥有领先的<代码>/,都将这样做。 为做到这一点,您可提取以下途径部分:parse_url(,并使用上述strtok(方法:

echo strtok(parse_url($url, PHP_URL_PATH),  / )




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签