Jump to content

[Question] How to check part of a string


kieran

Recommended Posts

I am trying to make an anti advertise script where server owners can put in known server names and parts of IP's, for example if you typed 'mtasa://192.192.192.192:22003' it would grab mtasa:// and cancel the message, outputting a warning to the sender of the message.  Currently it works when I type in mtasa://, but if I type anything before or after it doesn't detect it, I know the problem is how I am checking the string, I just have no clue how I could get an IP between words from the chat.

Script is below (still in early phases so it's messy and very simple.)

strings = {
	{'mtasa://', 'DO NOT ADVERTISE'}, --tablesting[1], outputmessage[2]
}

function checkText( message, messageType )
    if messageType == 0 then
		for i=1,#strings do --For all lines in table
			tablestring = strings[i][1] --Get the text to check
			outputmessage = strings[i][2] --And text to send back
			if ( string.gsub ( message,'','' ) == tostring(tablestring) ) then --if the message contains the checked text
				cancelEvent() --The event will be cancelled so the text is not seen
				outputChatBox(tostring(outputmessage), source, 255, 0, 0) --And the player will get the warning message
			end
		end
	end
end

addEventHandler("onPlayerChat", root, checkText)

I know it's line 10 and the problem is that I'm just getting the message and not a part of the message, the problem is that I don't want to just get the start of the message, I want to get the text from the table within the message, if that makes sense. xD so for example 'textmtasa://text' I just want to get the mtasa:// part.

Thanks for any help/advice.

Link to comment

@Fist Thanks so much!  For anyone wanting a sort of early version of the script I'm making or that's struggling for an answer, here is the working code.

strings = {
	{'mtasa://', 'DO NOT ADVERTISE'}, --tablesting[1], outputmessage[2]
}

function checkText( message, messageType )
    if messageType == 0 then
		for i=1,#strings do 
			tablestring = strings[i][1]
			outputmessage = strings[i][2]
			findstring = string.find(tostring(message),tostring(tablestring))
			if findstring then
				cancelEvent()
				outputChatBox(tostring(outputmessage), source, 255, 0, 0)
			end
		end
	end
end

addEventHandler("onPlayerChat", root, checkText)

 

Edited by kieran
Link to comment

Sorry can't edit last post, but now I want to take it further and convert uppercase letters to lowercase, I've done this and it works with %l, my issue is that I don't know how I'd send it as if the player typed it to all other players, is there a native function to output a message like this or would I need to get the players name and tag color and do it manually?  I know I can do just "outputChatBox(message)"  on server side and all will see it, but would I need to add name and color to the start of that message?  Or is their an easier way?

function checkCaps( message, messageType )
    if messageType == 0 then
		capsfree = string.match(tostring(message), '%l') --%l will check for lower case letters
		if not capsfree then --If there is no lower case letters
			cancelEvent()
			outputChatBox(string.lower(message)) --Then convert the string to lower case
		end
	end
end

addEventHandler("onPlayerChat", root, checkCaps)

 

Link to comment

You can make the search case insensitive by simply searching for lower case characters in a lowercased text as so:

string.find(string.lower(stringToSearchIn), substringToFind)
-- or
stringToSearchIn:lower():find(substringToFind) -- OOP style

As long as substringToFind is lower case, it will find lowercase substrings, but because everything it searches is lowercase, it will find the substring regardless of the case of the original stringToSearchIn (this also returns the start and end index, should you want to use it to index the original mixed-case stringToSearchIn using string.sub for example)

Edited by MrTasty
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...