Jump to content

Recommended Posts

So, I dint know much about reg ex and want to learn but the manual is hard to understand.

 

This is what I want...

 

I have a chatroom and I want to add the functionality of IRCs "/me" command.

 

so I thought preg_match would be best for this.

 

sample code:

$message = "/me is here!";
$pattern = "???";
if (preg_match($pattern, $message))
{
//Preg_Match insert into DB
}
else
{
//normal insert into DB
}

I need to see if "/me " are the first 4 chars of the string

Then I need to delete "/me " from the string and then insert the new string into the DB

Otherwise Insert the string as it is into the DB.

I have the insert functions working but I just need to do an if statement to see if /me is at the beginning of the string.

 

I think that is all the info I need to post for this forum, if not, let me know.

Link to comment
https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/
Share on other sites

What that second line does is use strpos to check to see if the character sequence '/me ' is there and starts at position 0 in the string (therefore, the beginning of the string). If so, then $message will be equal to itself starting at position 4 (which comes right after the space after /me) by use of substr, otherwise, it is simply equal to itself (nothing has changed).

 

Hopefully I explained that adequately.

 

EDIT - And of course, this is all done in a ternary operator fashion...as opposed to the typical

 

if(...){

  ...

} else {

  ...

}

Alternatively,

 

Example:

$message = "/me is here!";
$message = (strpos($message, '/me ') === 0)? substr($message, 4): $message;

How would I put that into an IF statement?

 

I basically need to insert into the database for the chat column "me" = yes when /me is used

 

How would I put that into an IF statement?

 

I basically need to insert into the database for the chat column "me" = yes when /me is used

 

 

You don't need to put it into an if, as it's a "ternary" (aka shorthand if),

 

Your could do this

$message = "/me is here!";
if (strpos($message, '/me ') === 0)
{
$message = substr($message, 4); //remove the "/me "
//Preg_Match insert into DB
}else{
//normal insert into DB
}

 

BUT if the insert into database is going to be the same, then this

 

$message = "/me is here!";
$message = (strpos($message, '/me ') === 0)? substr($message, 4): $message;

 

is the same as

$message = "/me is here!";
if (strpos($message, '/me ') === 0)
{
$message = substr($message, 4); //remove the "/me "
}
//normal insert into DB

 

Make sense ?

Yes it makes sense but it seems to break my code.

 

$charname = $_GET['char'];
$msg = $_GET['msg'];
$time = time();

$message = $_GET['msg'];
if (strpos($message, '/me ') === 0)
{
$message = substr($message, 4); //remove the "/me "
	$insert = "INSERT INTO chat (time, username, msg, me) values ('$time', '$charname', '$message', 'y')";
mysql_query($insert);
}
else
{
$insert = "INSERT INTO chat (time, username, msg) values ('$time', '$charname', '$msg')";
mysql_query($insert);
}
?>

 

I cant put actual data in the vars to make it easier for you. I just isnt inserting into to DB

If its not working i would guess you have a query problem

 

try this

$charname = $_GET['char'];
$msg = $_GET['msg'];
$time = time();

$message = $_GET['msg'];
if (strpos($message, '/me ') === 0)
{
$message = substr($message, 4); //remove the "/me "
$insert = "INSERT INTO chat (time, username, msg, me) values ('$time', '$charname', '$message', 'y')";
mysql_query($insert) or die(mysql_error()); //updated
}else{
$insert = "INSERT INTO chat (time, username, msg) values ('$time', '$charname', '$msg')";
mysql_query($insert);
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.