0
function splitsection($string,$start,$end) {
    return strstr( substr( $string, strpos( $string, $start) + strlen( $start)), $end, true);
}

I get the following error for some reason:

Warning: Wrong parameter count for strstr()

Any ideas?

Andrew
  • 18,680
  • 13
  • 103
  • 118
Peak Dermutag
  • 103
  • 2
  • 12
  • 2
    Possible duplicate: http://stackoverflow.com/questions/6954792/wrong-parameter-count-for-strstr – Reger Feb 15 '14 at 17:17
  • 2
    Check the [manual](http://dk1.php.net/strstr) that explains that in `5.3.0`: "*Added the optional parameter before_needle.*". That means that to use the last parameter you NEED to use at least PHP 5.3.0 or create your own function that does the same thing. – h2ooooooo Feb 15 '14 at 17:18
  • @tas9 I see yeah this is because of lower php version, Can anyone help make this function compatible with older versions? – Peak Dermutag Feb 15 '14 at 17:18
  • 1
    @PeakDermutag While a strategy like that may work, you will find it immensely hindering, especially when you try to do something that you *can't* do in older versions, like anonymous functions with `use`. – Niet the Dark Absol Feb 15 '14 at 17:21

4 Answers4

2

The PHP manual specifies that the $before_needle parameter first was added in 5.3.0. Therefore, if you use older versions you are applying one too many parameters. Worry not, though, as you can easily replicate the strstr function using strpos and substr to make it work in older versions of PHP (< 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);
        }
    }
?>

Usage:

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

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

DEMO

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0

I think you're using an old PHP version. The third parameter is not supported in PHP 5.2 and older. I suggest you use an newer version of PHP like version 5.3, 5.4, 5.5 or 5.6.

The PHP docs says:

5.3.0 Added the optional parameter before_needle.

0

Here is one more solution for YOU:

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

$email  = 'name@example.com';  
$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
?>
Amit Verma
  • 8,660
  • 8
  • 35
  • 40
-1

Update to PHP version 5.3 or newer.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592