Jump to content


Photo

pm command


  • Please log in to reply
5 replies to this topic

#1 derek.sullivan

derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 343 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 10 May 2011 - 07:53 PM

A while ago I posted a topic about how to do an action command, and the answer was to use preg_replace. Well that worked NOW my question is this:

If I want to send a private message to a user like such /pm Derek test
How would I split Derek (to be the person I send it to) and test (as the message I send to the person)

This is what I have thus far (just an example but it's the same concept):

<?php

$m = "/pm Derek test";
echo preg_replace("#/pm (.*?)+(.*?)#s", "\\1", $m);

?>

all that outputs is "Derek test"

any suggestions?

#2 derek.sullivan

derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 343 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 10 May 2011 - 08:39 PM

BTW, what I mean by split is:
to = Derek
message = test
so when it sends it will insert into differnt values in the mysql query:

$to = Derek;
$message = test;
mysql_query("INSERT INTO whatever (a,<img src='http://www.pixel2life.com/forums/public/style_emoticons/<#EMO_DIR#>/shades.gif' class='bbc_emoticon' alt='B)' /> VALUES('$to','$message')");
basically, I want /pm Derek test to end up in different variables

#3 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 10 May 2011 - 08:47 PM

rc is the regex god around here last I remember but...


I think it would be something like this.
/^\/pm ([A-Za-z0-9]+)\s{1}(.*)$/

and it would dump into an array of matches you set in the preg_match function. I did the name match on upper/lowercase letters and numbers just because I normally would not let it match anything in that spot just for security. Make it match only for what is allowed in the username.

Edited by Hayden, 10 May 2011 - 08:49 PM.


#4 derek.sullivan

derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 343 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 10 May 2011 - 10:09 PM

perfect! Thanks that did work! Does anyone know of a website that deals specifically with regex? I want to learn about all the characters and how to put them together.

#5 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 10 May 2011 - 10:34 PM

perfect! Thanks that did work! Does anyone know of a website that deals specifically with regex? I want to learn about all the characters and how to put them together.


I personally learned by doing. I used this cURL class to grab some HTML and just played with matching certain things on the page.

PHP.net and check out the regex cheat sheets at AddedBytes

#6 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 10 May 2011 - 11:49 PM

I pretty much agree with everything Hayden has said thus far (except maybe the part about being a god). However, my two cents:

1.
/^\/pm ([A-Za-z0-9]+) (.*)$/
The "\s{1}" in there was a little too complicated for what we're trying to do. By itself, "\s" is the same as "\s{1}" since they are both saying match one character. That character would be any kind of whitespace (tab, newline, etc...) and we only really want a space right?

2. Php.net is where i learnt regex from. However, it took a large effort on my part to sift through their documentation to figure out what i needed to figure out. Now that i have though, it is an incredible resource to refer back to. However, the other site i've poked around in a bit lately is http://www.regular-expressions.info/.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users