Jump to content

function to cut strings


madrazel

Recommended Posts

i just made this little fella:

 


function strcut($x,$y,$z) {
if ($z=="<-") return strstr($x,$y);
if ($z=="(-") return substr(strstr($x,$y),strlen($y),strlen($x));
if ($z=="->") return strrev(strstr(strrev($x),strrev($y)));
if ($z=="-)") return strrev(substr(strstr(strrev($x),strrev($y)),strlen($y),strlen($x)));
if ($z=="<") return $y.strrev(substr(strrev($x),0,strpos($x,$y)));
if ($z=="(") return strrev(substr(strrev($x),0,strpos($x,$y)));
if ($z==">") return substr($x,0,strpos($x,$y)).$y;
if ($z==")") return substr($x,0,strpos($x,$y));
};

echo strcut("abcXYdefXYghi","XY",")");


 

it works like this:

string: abcXYdefXYghi

 

<- = XYdefXYghi
(- = defXYghi
-> = abcXYdefXY
-) = abcXYdef
< = XYghi
( = ghi
> = abcXY
) = abc

 

any ideas to improve it ?

Link to comment
Share on other sites

sorry, i forgot...

 

function strcut($x,$y,$z) {
if ($z=="<-") { return strstr($x,$y); }
elseif ($z=="(-") {return substr(strstr($x,$y),strlen($y),strlen($x));}
elseif ($z=="->") {return strrev(strstr(strrev($x),strrev($y)));}
elseif ($z=="-)") {return strrev(substr(strstr(strrev($x),strrev($y)),strlen($y),strlen($x)));}
elseif ($z=="<") {return $y.strrev(substr(strrev($x),0,strpos($x,$y)));}
elseif ($z=="(") {return strrev(substr(strrev($x),0,strpos($x,$y)));}
elseif ($z==">") {return substr($x,0,strpos($x,$y)).$y;}
elseif ($z==")") {return substr($x,0,strpos($x,$y));}
};

Link to comment
Share on other sites

ok, now i done it,:

 

// x - haystack, y - needle, z - mode
function strcut($x,$y,$z) {
switch ($z) {
case '<-': return strstr($x,$y);
case '(-': return substr(strstr($x,$y),strlen($y),strlen($x));
case '->': return strrev(strstr(strrev($x),strrev($y)));
case '-)': return strrev(substr(strstr(strrev($x),strrev($y)),strlen($y),strlen($x)));
case '<' : return $y.strrev(substr(strrev($x),0,strpos($x,$y)));
case '(' : return strrev(substr(strrev($x),0,strpos($x,$y)));
case '>' : return substr($x,0,strpos($x,$y)).$y;
case ')' : return substr($x,0,strpos($x,$y));
default  :  echo "ERROR IN STRCUT"; break;};
};

//testing
$z = array('<-','(-','->','-)','<','(','>',')',);
$x = '123-XY-456-XY-789';
foreach ($z as $z) { echo $z.'  =   '.strcut($x,'XY',$z)."\n"; }

//output
//<-  =   XY-456-XY-789
//(-  =   -456-XY-789
//->  =   123-XY-456-XY
//-)  =   123-XY-456-
//<  =   XY-789
//(  =   -789
//>  =   123-XY
//)  =   123-

 

the question is actually about particular expresiions, i want to avoit things like these:

strrev(substr(strstr(strrev($x),strrev($y)),strlen($y),strlen($x)))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.