2012-03-15 112 views
0

我有这个网址:​​,我只想抓住你好和世界。如何获取PHP中URL的最后两部分?

我该怎么做?

谢谢!

+0

那你试试? – Evert 2012-03-15 17:21:17

+0

从这里开始:http://us3.php.net/manual/en/function.parse-url.php – 2012-03-15 17:22:11

+0

没什么,因为我不知道如何:( – user1219572 2012-03-15 17:22:13

回答

8
<?php 

$url = 'http://itutormaths.hub/home/hub/6/hello/world'; 

$pathParts = explode('/', parse_url($url, PHP_URL_PATH)); 

$lastParts = array(array_pop($pathParts), array_pop($pathParts)); 

var_dump($lastParts); 
+0

这只是一段性感的代码。干杯队友 – user1219572 2012-03-15 18:14:56

0
$url='http://itutormaths.hub/home/hub/6/hello/world';  
$res=explode("6/",$url);  
$res1=$res[1];  

$res=explode("/",$res1); 
$res1=$res[0]; 
print_r($res1); 
$res2=$res[1]; 
print_r($res2); 

使用爆炸功能

0
<?php 
//returns all occurrences of a string 
function strpos_recursive($haystack, $needle, $offset = 0, &$results = array()) {     
    $offset = strpos($haystack, $needle, $offset); 
    if($offset === false) { 
     return $results;    
    } else { 
     $results[] = $offset; 
     return strpos_recursive($haystack, $needle, ($offset + 1), $results); 
    } 
} 

$url = "http://itutormaths.hub/home/hub/6/hello/world"; 

$positions = strpos_recursive($url,"/"); 



//World (ie the 7th occurence of /) 
echo substr($url, $positions[6]+1)."<br />"; 

//hello (ie the string between the 6th and 7th occurence of /) 
echo substr($url, $positions[5]+1,($positions[6]-$positions[5]-1))."<br />"; 

?> 

或检查出preg_mactch_all(php.net/manual/en/function.preg-match-all.php)

相关问题