Reputation: 159
my string is
$str = ' 4/151n';
My regex is
preg_match_all('/[^!$|@|!|#|%|\^|&|*|(|)|_|\–|\-|+|=|\\|\/|{|}|\[0-9\]|.|,|\|:|;|"|\'|\s+|→|<|>|\~\[\\r\\n\\t\]~]/', $str, $matches);
My output is
Array
(
[0] => �
[1] => n
)
The output I need is
Array
(
[0] => n
)
Let me explain my requirement clearly. I need to remove all the special characters, numbers, new lines, white spaces from my string. The above regex is working fine for all cases except the above string.
For the above string, I am getting some unknown character in the 0th location. Need to remove that.
Please help me out!
Thanks in advance!
Upvotes: 1
Views: 93
Reputation: 784918
I need to remove all the special characters, numbers, new lines, white spaces
You can just use:
$str = preg_replace('/\P{L}+/u', '', $str);
//=> n
Here \P{L}+
matches 1 or more of non-letters with unicode support.
Update: Based on this comment from OP:
if I have multiple words in a string, I need to get them in an array
You can use:
if (preg_match_all('/\p{L}+/u', $str, $m))
print_r($m[0]);
Upvotes: 3