[SOLUTION] Is this preg_replace correct?
I want to let users add their twitter to my site, and twitter only allows aphanumeric characters + _ underscore in names.
What that line will do is remove any non-complaint characters from the user's input.
Please note that that's not the same as checking if the user has entered a correct username.
You would be saving the wrong username: andresdelfino (since you are removing the offending -).
I strongly suggest you to ask the user to correct his/her mistake should he/she make one.
Oh no, I dont give a sh*t about checking, Ill just sanitize than verify on twitter if exists. No time to waste with stupid warnings.
So is my preg-match correct or not?
Sorry, your solution is not correct.
What I'm suggesting you is to ask the user to re-enter their Twitter account username should you find a non-complaint character.
In case you prefer not to do this, I strongly suggest you to ignore usernames with non-complaint characters showing an error to the user, since you can't sanitize them at all. Your solution removes non-complaint characters from the user's input, but that doesn't guarantee you a valid username at all, just that the characters used are correct. The example I shared with you earlier shows this.
Will wipe all illegal characters clean. The correctness of username is than checked with twitter along with name, followers, country etc etc... So why would I display stupid message when I can just wipe and GUARANTEE correct entry. Or wrong username is cancelled out with twitter verification.
(notice you were lacking the replacement string, in this case "", and that the + sign must be after the closing square barckets).
Add + to your string (#[^a-zA-Z0-9_+]#), and yes, it will wipe all illegal characters clean.
Sorry, for your first post, I understood that Twitter also accepts "+" in usernames. But now I understand what you mean by it.
So, yes, your string is perfect.
I hate to burst your bubble... but like I said before, that + must be outside the square brackets.
Indeed, "+" must not be inside the square brackets. Like I said in my last post, I wrongly understood that "+" was a valid character, but later realized what the GVNPublic123 meant by saying "+" in his/her first post. That's why, in my last post, I stated that the correct string is the one GVNPublic123 shown in his/her first post.