Getting correct values out of string

I have string ($_POST) :

/ban user hours reason

/ban is chat command
user is some random username (with spaces)
hours is for how many hours user is banned
reason is explained text why user is banned (may contain spaces)

example: /ban Joe Bob Briggs 2 bad language

i need to separate all thees in 4 values;
1)/ban
2)Joe Bob Briggs
3)2
4)bad language

issue is spaces in username and reason :frowning:

How could i separate all this each time no matter how many spaces are there?

I don’t see that it can be done unless there are always the same number of spaces in the name. You need to use delimiters that won’t be used in a name or reason (a comma or semicolon) or encapsulate the values.

1 Like

How about if i would use /ban user hours [reason]

Then i have /ban on start , [some text] on end and number before [some text] so whats left in middle is username?

You could split up by the last number, from there everything before until the slashed word is a name. But that’s a very fragile approach that would immediately break on several conditions, e.g. reason contains a number. So the solution from @Gandalf is way better.

1 Like

As gandalf458 posted, there would need to be consistency of all possible strings. If there is I think regex could work. eg.

starting with slash up to first space
then letters up to numbers
the numbers
rest of letters

1 Like

the problem is that username can conain numbers and even symbols for now

How could i be able to read from /ban user hours [reason]

from start /ban

from end to right [reason] and 2

leaving all in middle as username?

with strrpos() / substr() or use regex.

1 Like

I m bad with them yet. can i ignore numbers inside [reason] and look for first number from end after [reason] ?

supply a definition for [reason]

I m this far:

$string = '/ban user name 3 [this is reason]';

$a = substr($string, 0, strpos($string, ' '));

preg_match_all("/\\[(.*?)\\]/", $string, $b);
$b = $b[1][0];

echo $a.'<br>'; // returns: /ban
echo $b.'<br>'; // returns: this is reason

The issue is if username or text of reason will contain it will broke it :frowning:

That’s because you did not provide a concept of how the information is seperated logically.

But as you already alter the format anyway: why don’t you use a delimiter?

because form need to be /ban user hours [reason] it cant be /ban, user, hours, [reason]

So the input is invalid when it contains [] in any part that is not in the end - why is that your problem to fix invalid input? Tell the user what format you expect.

I just do not want to limit users with that (maybe i should) i was considering not to allow spaces in username also

Then you first must define what you expect the user to input. But that’s less a technical question, more a conceptual. If you just want your tool to analyze any given string, try machine learning / AI.

After all this i think i will limit user to use only letters and numbers!

Can’t you pass the user-id instead of the name? Then you wouldn’t need to limit what your users can have in their username, because you would only use the auto-increment id from the users table. Or do you use the user-id string for all internal stuff too?

Its just that in chat box moderator do not know user id and he post /ban user 1 rules

Alter moderators chat box display so it shows the user id as well as the name? Give the mod a pop-up form to fill in?

Command line interpreters have the same issues, anything that tries to intelligently figure out which words should be grouped and which should not generally gets it wrong some of the time, and they get around it by insisting grouped words are quoted. In fact browsers are the same, there was a topic here yesterday where a html value containing spaces “didn’t work” because the code didn’t surround it in spaces. The fact that browser developers don’t try to handle this stuff tells you it must be too complex to be worth doing.

1 Like