Tags
PHP
Asked 8 years ago
30 Sep 2016
Views 1812
debugger

debugger posted

Warning: Wrong parameter count for strstr() in php

Hi

i am using strstr for skipping some string . means want to get part of string .

suppose

$email="something@example.com"
$user = strstr($email, '@', true); 


it give me error
Warning: Wrong parameter count for strstr() in php

so help me to find what is wrong ?
which php version you using with ? - Mitul Dabhi  
Sep 30 '16 08:16
@Mitul its PHP Version 5.2.9 - debugger  
Sep 30 '16 08:17
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00



before php version - 5.3.0 , strstr have only two parameter to pass
syntax :: strstr ( string $haystack , mixed $needle )
at php version - 5.3.0 , strstar have three parameter , adde one optional parameter "before_needle"
syntax :: strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

so pls upgarde php version to 5.3.0 or remove last argument "true" to use in same environment.

$user = strstr($email, '@') or upgarde version to php 5.3.0
Thanks
Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00

if you dont want to upgarde than use below custom strstr with $before_needle parameter for lower php version - 5.3.0


//custom function for strstr with 
function strstr_($haystack ,$needle,$before_needle){
  if($before_needle)
	return str_replace(strstr($haystack ,$needle),"",$haystack );
else 
	return strstr($haystack ,$needle);
 }

//use same as strstr for php 5.3.0
echo strstr_($url_piece,"/",true); 
shyam

shyam
answered Nov 30 '-1 00:00

lets use of skip strstr in custom function also



//strstr function compitable with latest option before_needle without use of strstr 
function str_str($haystack,$needle,$before_needle=false)
{
 if($before_needle)
 return substr($haystack,0,strpos($haystack, $needle));
else 
return substr($haystack,strpos($haystack, $needle));
 }
Post Answer