Page 1 of 1
String help
Posted: Mon Feb 28, 2005 4:33 pm
by cbrian
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.
Posted: Mon Feb 28, 2005 4:35 pm
by Chris Corbyn
There's few functions that would help you if you look at the manual.
substr() and the preg_....() functions (read on regular expressions to get a good insight into string manipulation).
Posted: Mon Feb 28, 2005 4:37 pm
by cbrian
Thanks, I'll go check out the PHP manual.
Posted: Mon Feb 28, 2005 4:37 pm
by feyd
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)
Posted: Mon Feb 28, 2005 5:38 pm
by cbrian
Would it be possible to use explode() to seperate it into three pieces, one, before the first space, two, between the first and second spaces, and three, everything after the third space(including spaces)?
Posted: Mon Feb 28, 2005 5:44 pm
by feyd
explode() does all matches of the search string... so you'd have to recombine all the remaining ones back together.. but yes, it's possible to do it..
Posted: Mon Feb 28, 2005 6:28 pm
by cbrian
Sorry, I just can't get this part.
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]
Posted: Mon Feb 28, 2005 6:41 pm
by Chris Corbyn
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.
Code: Select all
$num_parts = count ($marray);
for ($i=2; $i<$num_parts; $i++) {
//do what you'd do in a while loop
}
Posted: Mon Feb 28, 2005 7:02 pm
by cbrian
Thanks. I just decided to use this because I just don't understand regular expressions.
Posted: Mon Feb 28, 2005 7:03 pm
by feyd
don't worry about not understanding regular expressions.. most people here don't understand them either (including some senior programmers

)
Posted: Mon Feb 28, 2005 7:09 pm
by Chris Corbyn
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.
Posted: Mon Feb 28, 2005 7:21 pm
by cbrian
Do you know of any good tutorials on them?
Posted: Mon Feb 28, 2005 7:25 pm
by feyd
search here.. we've linked to many in the past, along with links to several helpful books and tools are listed in the PHP Starter Pack thread (top of this board)
Posted: Mon Feb 28, 2005 7:27 pm
by cbrian
Thanks, I'll check it out.