0

So I'm busy building a site and trying to test out a sort of filter for certain words but trying to determine which is the best function to use and through what language. I've done a bit of research and in PHP I can use the strpos() function so for example:-

if (strpos($checkstring, 'geordie') !== false) {
   $checkstring = 'invalid name';
 }

I'm unsure as to whether there is a decent JQuery function that could be used to achieve the same thing. Basically I want to be able to block my friends from using my name or nickname so it would include any and all variations of 'geordie' including lowercase and uppercase as well as getting past it using 'GeoRdie' or something to that affect but also want to stop variations which would be my full nickname 'geordie dave' or 'geordie ****' or even 'geordie dave is a ****'.

I realise that this is probably a bit of a complicated one but there must be a way using perhaps an array of names?

Any help on a function to use would be great and if anyone could possibly give me an example of code that could be used would also be beneficial.

1
  • Thanks guys! I shall do it on both sides. Cheers for the advice :) Commented Jul 28, 2013 at 12:07

3 Answers 3

2

You should probably do it in javascript and in php (client side and server side). The javascript eqivalent of strpos is indexOf. If you only check with javscript, someone could forge a post packet and it would still be accepted by the server. If you are only going to check in one place, make it server side, but for user-friendly-ness, both is preferred.

Sign up to request clarification or add additional context in comments.

Comments

2

In JavaScript, you can use String#indexOf(String) to match exact strings, or RegExp#test(String) for more complicated matching.

if (str.indexOf("geordie") !== -1) {
    // `str` contains "geordie" *exactly* (doesn't catch "Geordie" or similar)
}

if (/geordie/i.test(str)) {
    // `str` contains "geordie", case-insensitive
}

And I'll second what Alfie said: You can't just do this on the client, because client requests can be spoofed. Client-side validation is purely for making a nice user experience; server-side validation is always required.

Comments

1

I think that you should also use PHP strtolower function on $checkstring variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.