String help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

String help

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Thanks, I'll go check out the PHP manual.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post 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)?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post 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]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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++) &#123;
    //do what you'd do in a while loop
&#125;
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Thanks. I just decided to use this because I just don't understand regular expressions.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

don't worry about not understanding regular expressions.. most people here don't understand them either (including some senior programmers ;))
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Do you know of any good tutorials on them?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Thanks, I'll check it out.
Post Reply