note 99281 added to function.explode

From: Date: Sun, 08 Aug 2010 13:41:19 +0000
Subject: note 99281 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-171463@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.197 Probable Submitter: 76.235.223.16 ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- https://master.php.net/note/edit/99281 Del: integrated -- https://master.php.net/note/delete/99281/integrated Del: useless -- https://master.php.net/note/delete/99281/useless Del: bad code -- https://master.php.net/note/delete/99281/bad+code Del: spam -- https://master.php.net/note/delete/99281/spam Del: non-english -- https://master.php.net/note/delete/99281/non-english Del: in docs -- https://master.php.net/note/delete/99281/in+docs Del: other reasons-- https://master.php.net/note/delete/99281 Reject -- https://master.php.net/note/reject/99281 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#171463) next »