0

Possible Duplicate:
PHP remove special character from string

I want to find a regex in this way:

I have the body of a SMS message in a $sms variable. I would be have as result a string with:

- parenthesis
- the non alphanumeric character )([]#@*^?!|"&% and remove the other that remain
- all alphanumeric character and numeric

this because I have some problem with my sms gateway machine that it not deliver the message with this character ° frequently used in Italy language to enumerate numbers.

I need to use PHP to do this, but only the regex can be a great help.

2
  • can you provide before and after examples. Commented Oct 9, 2012 at 23:55
  • So this isn't really a Question, but a request, are you trying to replace or match?, also you can post what code you have managed to write Commented Oct 9, 2012 at 23:56

1 Answer 1

0
$newSMS = preg_replace('/[^)(\[\]#*^?!|"&%a-zA-Z0-9]/', '', $sms);

The regular expression is matching any one character ([...]) which is not (^) among the characters you described ()(\[\]#*^?!|"&%a-zA-Z0-9) and replacing them with an empty string '', which is essentially the same as deleting them. This regex can be shortened to:

/[^)(\[\]#*^?!|"&%a-z\d]/i

by using \d to stand in for 0-9, and case-insensitive mode (i) so that we don't have to repeat the letters, but the savings are minimal, especially because PHP's digits are ASCII only anyway.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.