note 99281 deleted from function.explode by salathe

From: Date: Mon, 21 Sep 2020 14:49:54 +0000
Subject: note 99281 deleted from function.explode by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215338@lists.php.net to get a copy of this message
Note Submitter: Cody G. ---- I'm sure you guys get just a bit frustrated at times when you need a fraction of a very simple string and you use "explode()", but then you have to define a whole extra variable. (That is because you need to store a function-returned array in a variable before you can extract a value). If you're extracting the last half, or third, of a string, there's an easy inline workaround. Check this: <?php $mystr = "separated-text"; print(str_replace("-","",strstr("-",$mystr))); //Returns "text" ?> If the separator (dash) can be left in, you don't even need the "str_replace()" function. Lets try this with 3 fractions: <?php $mystr = "separated-text-again"; //Comment submission wouldn't let me // combine this into one statement. // That's okay, it's more readable. $split1 = str_replace("-","",strstr("-",$mystr)); print(str_replace("-","",strstr("-",$split1))); //Returns "again" ?> Anything more than 3 fractions gets really confusing, in that case you should use "explode()". Hope this helps! ~Cody G.

« previous php.notes (#215338) next »