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?
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?
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"
?>
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.
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
?>
Update to PHP version 5.3 or newer.