asmith Posted February 23, 2009 Share Posted February 23, 2009 Hi, I'm looking for this syntax. I wanna explode this string, and I need like the 3rd value on the array : <?php $a = 'The text must be exploded'; $exploded = explode(' ',$a); echo $exploded[2]; ?> I wanna know, is it possible I echo it without putting it in the $exploded? I mean directly, something like : echo explode(' ',$a)[2]; Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 23, 2009 Share Posted February 23, 2009 not that i'm aware of...is there a specific reason? this doesn't really help, but it's another trick: <?php $a = 'The text must be exploded'; list(,$value) = explode(' ',$a); echo $value; //prints 'text' ?> Quote Link to comment Share on other sites More sharing options...
asmith Posted February 23, 2009 Author Share Posted February 23, 2009 No big reason for that. I don't need that variable anywhere in the script, so I was wondering about not making it at all. Personally I love scripts as short as possible. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 23, 2009 Share Posted February 23, 2009 You could use Regular Expression. But, I think what you might gain in compact code you would lose in "efficiency" of the code. Quote Link to comment Share on other sites More sharing options...
Philip Posted February 23, 2009 Share Posted February 23, 2009 This works, but really isn't as efficient as just creating a variable $a = 'The text must be exploded'; echo implode(array_slice(explode(' ',$a),2,1)); Quote Link to comment Share on other sites More sharing options...
asmith Posted February 23, 2009 Author Share Posted February 23, 2009 yes true. I always thought there is a simple syntax for it. Thanks all for your time Quote Link to comment Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 No big reason for that. I don't need that variable anywhere in the script, so I was wondering about not making it at all. Personally I love scripts as short as possible. Honestly clean code is nice, but easy to read code is better. I tend to keep my code where people can know what is going on if someone is coming in behind me or to help me. Memory is abundant, so you might as well use it, especially with a simple thing as a variable or an array of variables. The list is good for what you want, but say it explodes 50 characters and you never know which one you want (IE it is not always the second one). The array would be much easier/better imo for that situation. It all depends on your ultimate goal. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.