Jump to content

How to look for a word in a string


ILYAS415

Recommended Posts

Okay i'm making a chat room thing and i would like to know how check if the user has entered a certain word into his her string.

 

Basically im trying to make a /me commad (the typical action command).

 

I have already tried using this....

 

strpos("/me", $string, 1);

 

however that means that the user can enter in....

 

wow the /me command is wicked

 

and if will be seen as an action command because it got /me inside it.

I only want the script to look for /me if it is in the beggining of a sentance.

Link to comment
https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/
Share on other sites

Barand your code didnt appear to work for me.

 

phpSensai im not sure how exactly to use the function your gave me. Can you please give me a snippet based on the example i gave?

 

hmm im considering bbcodes as my last option.

Nope not working... :S

 

heres my code im using...

 

 

	if (strpos($msg, "/me") === "0"){
	$msg= str_replace("/me", "", $msg);
	$out="$chattext ".ucfirst($fetch->username)." <font color=white>$msg</font><br>";
	}else{
	$out = $chattext . $n . " - " . $msg . "<br>";
	$out = str_replace("\'", "'", $out);
	$out = str_replace("\\\"", "\"", $out);
	}
	$out= chat($out);

 

Thanks again

Don't have $fetch object so I changed $fetch->username to $username.

Also don't have you chat() function, but

 

<?php
        $username = 'xyz';
        $msg = '/me foo bar';
        $chattext = 'abc';
         
        if (strpos($msg, "/me") === 0){
	    $msg= str_replace("/me", "", $msg);
	    $out="$chattext ".ucfirst($username)." <font color=white>$msg</font><br>";
	}else{
	    $out = $chattext . $n . " - " . $msg . "<br>";
	    $out = str_replace("\'", "'", $out);
	    $out = str_replace("\\\"", "\"", $out);
	}
	echo $out;                      // --> abc Xyz <font color=white> foo bar</font><br>
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.