gergy008 Posted December 23, 2010 Share Posted December 23, 2010 I am in need of a alimit on the explode() fucntion but reading through the php explode manual on php.net there doesn't seem to be one that is right for me. Basically I'm making a command system for a php chat and it has a command called ban. I have code that find out if it is a command or a normal string, I also have code that starts to break up the command into segments that can be places in the database. I want to split up "ban user lengthID "Reason why you were banned"" using explode($string, " ") but I notice that that will also break up the "reason why you are banned". How do I get it to split up the command but NOT thereason why you were banned string? Confusing I know, Please ask any questions that would help me explain it better Thanks in advance! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 23, 2010 Share Posted December 23, 2010 The third argument to the explode function will do what you want (if I understand what you want): <?php $str = 'ban user lengthID "Reason why you were banned"'; $exp = explode(' ',$str,4); echo '<pre>' . print_r($exp,true) . '</pre>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 i didn't know about that last parameter. i took a look and also found this in the manual: If the limit parameter is negative, all components except the last -limit are returned. This feature was added in PHP 5.1.0. so to always leave off "Reason why you were banned", you might do something like this: $str = 'ban user lengthID "Reason why you were banned"'; $exp = explode(' ',$str,-5); // Don't grab the last 5 words echo '<pre>' . print_r($exp,true) . '</pre>'; http://php.net/manual/en/function.explode.php Quote Link to comment Share on other sites More sharing options...
gergy008 Posted December 23, 2010 Author Share Posted December 23, 2010 The third argument to the explode function will do what you want (if I understand what you want): <?php $str = 'ban user lengthID "Reason why you were banned"'; $exp = explode(' ',$str,4); echo '<pre>' . print_r($exp,true) . '</pre>'; ?> Ken I misunderstood the example on the website, Thanks Ken. To my understanding, If the limit is positive then it will stop on the value? Eg. if I used explode(" ", 'this text is text but stop now', 5) in theory would come out as: Array ( [0] => this [1] => is [2] => text [3] => but [4] => stop now ) ? (But yeah thanks exacly what I needed ) 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.