2012-07-27 48 views

回答

2

你可以使用strpos()

$mystring = 'abc'; 
$findme = 'a'; 
$pos = strpos($mystring, $findme); 

// The !== operator can also be used. Using != would not work as expected 
// because the position of 'a' is 0. The statement (0 != false) evaluates 
// to false. 
if ($pos !== false) { 
    echo "The string '$findme' was found in the string '$mystring'"; 
     echo " and exists at position $pos"; 
} else { 
    echo "The string '$findme' was not found in the string '$mystring'"; 
} 
1

你可以把URL並以斜線分割 - 使用.explode()函數。

$url = 'http://abv.com/en/'; 
$urlParts = explode('/',$url); 
array_shift($urlParts); 
array_shift($urlParts); 

使用array_shift()兩次你刪除不需要的http和空白項目由於雙斜槓...

Array 
(
    [0] => abv.com 
    [1] => en 
    [2] => 
) 

.parse_url()也有處理URL字符串一些有用的功能。你應該檢查出來。

$url = 'http://abv.com/en/'; 
$urlParts = parse_url($url); 
$pathParts = explode('/',$urlParts['path']); 
1

要做到這一點,最簡單的方法是使用strpos()

if (strpos($url, '/en/') !== false) { 
    // found! 
} 

如果你想檢查人的路,不過,使用parse_url()會有所幫助:

if (strpos(parse_url($url, PHP_URL_PATH), '/en/') !== false) { 
    // found in the path! 
} 
1

你可以使用php explode函數分開url,然後檢查url是否有「en」(國家代碼)。

$url = 'http://abv.com/en/'; 
      $expurl = explode('/', $url); 
      print_r($expurl);    
      foreach ($expurl as $key => $value) { 
      if ($value == 'en') { 
       # do what you want 
      } 
      } 

陣列導致

Array ([0] => http: [1] => [2] => abv.com [3] => en [4] =>)