Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would you shorten this function? 2

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi guys :)

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 :)
 
always up for a challenge. can you provide a couple of lines on what you are trying to achieve in the function?
 
Hi Jpadie ;)

As you can see in the echoed part at the botttom, the function is equivalent to strstr() but with more options such as :
- returns either the left or right part around the delimiter
- use the first or last occurence of the delimiter.
- keep or ommit the delimiter in the output.

I just want to know how a real coder could produce the same function with less lines of code.
 
when you say keep the delimiter do you really mean it to output the way you say? so if you say you do not want delimiters to be output, you actually do want them but just not at the extremities? seems odd.

i'm down to 12 lines with reasonable line spacing, by the way.
 
ok, i've reached the end of my challenge limit!

here is the function the way i think it should work
Code:
<?php
function hd_strstr($string, $output_pos="LEFT", $delim_incl="WITHOUT" ,$delim_pos="FIRST",$delim_string="@") {
	$split = explode($delim_string, $string);
	$glue = ($delim_incl=="WITH") ? $delim_string : ''; 
	if ($output_pos == "RIGHT"){
		if($delim_pos == 'FIRST'){unset($split[0]); return $glue.implode($glue, $split);}else{return $glue.$split[count($split)-1];}
	}else{	
		if($delim_pos == 'FIRST'){return $split[0].$glue;}else{array_pop($split); return implode($glue, $split).$glue;}
	}
}
?>

if you really want it the way you said (with delimiters appearing even when you say you don't want them) then this code will do it
Code:
<?php
function hd_strstr($string, $output_pos="LEFT", $delim_incl="WITHOUT" ,$delim_pos="FIRST",$delim_string="@") {
	$split = explode($delim_string, $string);
	$glue = ($delim_incl=="WITH") ? $delim_string : ''; 
	if ($output_pos == "RIGHT"){
		if($delim_pos == 'FIRST'){unset($split[0]); return $glue.implode($delim_string, $split);}else{return $glue.$split[count($split)-1];}
	}else{	
		if($delim_pos == 'FIRST'){return $split[0].$glue;}else{array_pop($split); return implode($delim_string, $split).$glue;}
	}
}

?>
 
oh ... any by the way I wouldn't call myself a real coder (which you were looking for in your second post) - i'm just a lawyer who dabbles in code ...
 
Wow ... that's very impressive.
Your second function does exactly what my function does and the code is so short! :)

Too bad I can't understand it ahaha :(
If you're not a real coder, I wonder what I am then ;)
 
sorry about the lack of commentary. here is the code again with inline explanations

Code:
<?
function hd_strstr($string, $output_pos="LEFT", $delim_incl="WITHOUT" ,$delim_pos="FIRST",$delim_string="@") {
	//first turn the string into an array, split at the delimiters
	$split = explode($delim_string, $string);
	//create a variable for later used based on the value of the delimiter and
	//the user choice of with/without.
	//use the ternary operator to do this
	//in this way we do not need to test later for the user's choice of delimiters
	$glue = ($delim_incl=="WITH") ? $delim_string : ''; 
	//create a nested if to deal with the remaining scenarios
	if ($output_pos == "RIGHT"){
		if($delim_pos == 'FIRST'){
			unset($split[0]); //remove the first item in the array (this would be the characters before the first delimiter
			return $glue.implode($delim_string, $split);	//collapse the new array back to a string and prepend the variable delimiter
		}else{
			return $glue.$split[count($split)-1];	//take the last item in the array (which is always count-1) and prepend it with the variable delimiter.
		}
	}else{	
		if($delim_pos == 'FIRST'){
			return $split[0].$glue;	//return the first item in the array (which will be the chars before the first delimiter) and suffix the variable delimiter
		}else{
			array_pop($split); //unset the last array element.  i could also have use unset($split[count($split-1)])
			return implode($delim_string, $split).$glue;}	//collapse the array back to a string, putting the delimiters back in and then add the variable delimiter to the end of the string
	}
}

?>
 
A worthy effort indeed jpadie! Thanks for being so thorough by giving an explanatory version too. I'm sure many (including myself) can learn from the approach you have used above. Keep up the good work!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thanks for the explanation :)

I really need to work on that difficult ternary operator :(

My problem is that I make the effort to learn new practices only when I have no other option left. So, in the end, I keep using my long and primitive code. More a question of productivity than laziness, though.
 
the ternary operator is dead simple. it's really just a syntax simplification rather than a novel function.

Code:
$output = (test) ? (if tru) : (if false);

you could equally phrase this as

Code:
if (test){
  $output = (if try)
} else {
  $output = (if false);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top