First you need to learn about regular expressions. They represent a general programming concept, and there is better place to read than the PHP manual. Try regular-expressions.info .
For your specific question, you will need a character class template: [...]
will match any of the characters inside the brackets. If you write it with a hat at the beginning, it will match any character that is not in brackets: [^...]
. You want to replace all non-matching characters with nothing, so you can use the preg_replace
function:
preg_replace("/[^...]/gu", "");
Traits are required for delimiters (slashes are traditional, but there are other things you can use), "g" means "global", like "replace all occurrences, not just the first one", and "u" means "unicode", which allows you to catch arabic characters and this strange special character at the end.
Now you can list all the characters where I put the dots; or you can specify ranges of characters. For example, [^a-zA-Z0-9,.\/+&-]
matches any alphanumeric English character and all special characters except the weird one at the end: p. Note that you need to avoid the backslash (because otherwise it will cause the regular expression to stop), and you must have a minus sign as the last one (otherwise it will be interpreted as a range of characters). You can distribute to other languages as needed (I am not familiar enough with the Arabic encoding).
source share