2014-02-15 32 views
0
function splitsection($string,$start,$end) { 
    return strstr(substr($string, strpos($string, $start) + strlen($start)), $end, true); 
} 

我收到以下錯誤出於某種原因:爲什麼錯誤「strstr()的參數計數錯誤」?

Warning: Wrong parameter count for strstr() 

任何想法?

+2

可能的重複:http://stackoverflow.com/questions/6954792/wrong-parameter-count-for-strstr – Reger

+2

檢查[手冊](http://dk1.php.net/strstr),解釋說,在'5.3.0':「*添加了可選參數before_needle。*」。這意味着要使用最後一個參數,至少需要PHP 5.3.0或創建自己的函數來執行相同的操作。 – h2ooooooo

+0

@ tas9我看到這是因爲較低的PHP版本,任何人都可以幫助使這個功能兼容舊版本? –

回答

2

PHP manual指定首先將$before_needle參數添加到5.3.0中。因此,如果您使用較舊的版本,則會應用一個太多的參數。別擔心,不過,你可以使用strpossubstr,使其在舊版本的PHP的工作容易複製strstr功能(< 5.3.0):

<?php 
    function strstr_replica($haystack, $needle, $beforeNeedle = false) { 
     $needlePosition = strpos($haystack, $needle); 

     if ($position === false) { 
      return false; 
     } 

     if ($beforeNeedle) { 
      return substr($haystack, 0, $needlePosition); 
     } else { 
      return substr($haystack, $needlePosition); 
     } 
    } 
?> 

用法:

<?php 
    $email = '[email protected]'; 
    $domain = strstr_replica($email, '@'); 
    var_dump($domain); //string(12) "@example.com" 

    $user = strstr_replica($email, '@', true); 
    var_dump($user); //string(4) "name" 
?> 

DEMO

-1

更新到PHP版本5.3或更新版本。

+0

@Neit我沒有這個選項,因爲即時共享主機 –

+0

然後更改主機。我工作的主機提供共享主機,並有一個下拉菜單來選擇5.2,5.3或5.4,甚至很快就會增加5.5。 –

0

我想你使用的是舊的PHP版本。 PHP 5.2及更早版本不支持第三個參數。我建議你使用更新版本的PHP,如版本5.3,5.4,5.5或5.6。

的PHP docs說:

5.3.0新增可選參數before_needle。

0

這裏有一個更適合你的解決方案:

<?php 
//Returns Part of Haystack string starting from and including 
//the first occurrence of needle to the end of haystack. 

$email = '[email protected]'; 
$needle = '@'; 
$domain = strstr($email, $needle); 
echo $domain.'<br />'; 
// prints @example.com 


//Returns Part of Haystack The way YOU want pre PHP5.3.0 
$revEmail = strrev($email); 
$name = strrev(strstr($revEmail, $needle)); 
echo $name.'<br />'; 
echo substr($name,0,-(strlen($needle))); 
// prints name 
?>