String help
Moderator: General Moderators
String help
I'm trying to write a PHP chat room, and I'm working on making a PM system. It will be in this format:
/msg username message
How could I sense if /msg is at the front, and the username, and then the message?
Sorry, I've just never had an opportunity to work with strings.
Thanks a lot.
/msg username message
How could I sense if /msg is at the front, and the username, and then the message?
Sorry, I've just never had an opportunity to work with strings.
Thanks a lot.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
yeah.. regular expressions is pretty much where it'd be.. you'd likely need them at least to make sure the username is a valid username at least.. I've made a lot of posts regarding explaining patterns and helping with regular expressions.. several are linked to in the Useful Posts thread (link in my signature)
Sorry, I just can't get this part.
So far, I have
So far, I have
Code: Select all
їphp]
if($amessage = stristr($message, '/')) {
$marray = explode(" ",$amessage);
if($marrayї0] == "/msg") {
$to = $marrayї1];
/*
Below this, what I'm trying to do is to make $marrayї2] become $marrayї0] and so on. But it makes an infinite loop and doesn't do what I'm trying to get it to do.
*/
$i = 2;
while($marrayї$i]) {
$h = $i - 2;
$marrayї$h] = $marrayї$i];
}
$rmessage = implode(' ', $marray);
} //end if /msg
} //ends message stristr
ї/php]- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
It's getting a bit messy using explode. The great thing with regular expressions (preg_match() ) is that you can actually specifiy very precisely the parts you want to check and the parts you want to extract.
As far as the while() loop goes the loop is infinite because the condition is always true.
If you really want to go down that road then use count() to find out how big the array is then use a for loop with the appropriate number of iterations.
As far as the while() loop goes the loop is infinite because the condition is always true.
If you really want to go down that road then use count() to find out how big the array is then use a for loop with the appropriate number of iterations.
Code: Select all
$num_parts = count ($marray);
for ($i=2; $i<$num_parts; $i++) {
//do what you'd do in a while loop
}- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
They can get/look pretty deep but if you learn the basics purely for matching and not necessarily replacing they all fall together in the end. I was first introduced to them in perl but I think you just need to kinda learn how to think about them. There's ways to get by w/out them but if you can do them they speed things up a lot.