Jump to content

Explode limit?


gergy008

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/222518-explode-limit/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/222518-explode-limit/#findComment-1150851
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/222518-explode-limit/#findComment-1150852
Share on other sites

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 ^_^)

Link to comment
https://forums.phpfreaks.com/topic/222518-explode-limit/#findComment-1150853
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.