Hi guys
Just out of curiosity, is it possible to shorten the code of this function I just made ? :
Thanks
Just out of curiosity, is it possible to shorten the code of this function I just made ? :
Code:
function hd_strstr($string, $output_pos, $delim_incl, $delim_pos, $delim_string) {
// $delim_incl : WITH / WITHOUT
// $delim_pos : FIRST / LAST
// $output_pos : LEFT / RIGHT
if (strpos($string, $delim_string)) {
if ($delim_pos == "FIRST") {
if ($output_pos == "RIGHT") {
$string = strstr($string, $delim_string);
if ($delim_incl == "WITHOUT") $string = substr($string, 1);
}
if ($output_pos == "LEFT") {
$string = str_replace(strstr($string, $delim_string), "", $string);
if ($delim_incl == "WITH") $string .= $delim_string;
}
}
if ($delim_pos == "LAST") {
if ($output_pos == "RIGHT") {
$string = strrev(str_replace(strstr(strrev($string), $delim_string), "", strrev($string)));
if ($delim_incl == "WITH") $string = $delim_string . $string;
}
if ($output_pos == "LEFT") {
$string = str_replace(strrev(str_replace(strstr(strrev($string), $delim_string), "", strrev($string))), "", $string);
if ($delim_incl == "WITHOUT") $string = substr($string, 0, -1);
}
}
}
return $string;
}
$string = "1@2@3";
echo "
" . hd_strstr($string, "RIGHT", "WITH", "FIRST", "@") . /* returns @2@3 */ "<br />
" . hd_strstr($string, "RIGHT", "WITHOUT", "FIRST", "@") . /* returns 2@3 */ "<br />
" . hd_strstr($string, "LEFT", "WITH", "FIRST", "@") . /* returns 1@ */ "<br />
" . hd_strstr($string, "LEFT", "WITHOUT", "FIRST", "@") . /* returns 1 */ "<br />
<br />
" . hd_strstr($string, "RIGHT", "WITH", "LAST", "@") . /* returns @3 */ "<br />
" . hd_strstr($string, "RIGHT", "WITHOUT", "LAST", "@") . /* returns 3 */ "<br />
" . hd_strstr($string, "LEFT", "WITH", "LAST", "@") . /* returns 1@2@ */ "<br />
" . hd_strstr($string, "LEFT", "WITHOUT", "LAST", "@") . /* returns 1@2 */ "<br />
";
Thanks