Forum Moderators: coopster

Message Too Old, No Replies

How to bring explode function into one combined line?

         

toplisek

10:58 am on Jan 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can be combined this rules?
$mytags_explode = explode(' ', $tag1 [0]);
$tag2 = ucfirst($mytags_explode['0']);

How to bring explode function into one combined line?

I have code into two lines.

rocknbil

4:47 pm on Jan 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm a little confused because you're using limit, which is 1,

If the limit parameter is zero, then this is treated as 1.


... so there should only be one item in the array if I interpret it correctly. But going with it, did you try this?

$tag2 = ucfirst(explode(' ', $tag1 [0])[0]);

Matthew1980

7:45 pm on Jan 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I would be interested to see the outcome to this, I am all for condensing code into more elegant chunks, but some functions don't like this being done; I had this happen a while ago and it surprised me to see that this [webmasterworld.com] was the cause of my issue here.

Sometimes for the use of debugging you need to have some pieces of code structured in certain ways so that echoing returned data/arrays is easier..

Cheers,
MRb

coopster

7:31 pm on Jan 21, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$tag2 = ucfirst(explode(' ', $tag1[0])[0]);

You'll get a parse error. This will work, but it breaks STRICT standards:
$tag2 = ucfirst(array_shift(explode(' ', $tag1[0]))); 
// Strict Standards: Only variables should be passed by reference ...

If you really must reduce two lines of code you could use a combination of substring and strpos, or even a preg_replace.
$tag2 = preg_replace('/(\S+)\s.*/e', "ucfirst('$1')", trim($tag1[0]));