Reputation: 119
Hello i tried below code using regex
$str = preg_replace("/[^a-z0-9_]/i", '', 'New_text % *');
//output => New_text
( _ is except char )
all work perfect but when my input string something like in other language(ex Hindi), char in Hindi Lang will also delete.
same as above example
$str = preg_replace("/[^a-z0-9_]/i", '', 'कपिल शर्मा % * _');
//output => _
how to get: कपिल शर्मा _
is there any mistake in regex or any other way in PHP we can do?
Upvotes: 3
Views: 2601
Reputation: 627469
You need to use
'~[^\p{M}\w]+~u'
See the regex demo
It seems that PHP PCRE regex does not match combining marks with \W
and /u
modifier, so, we need to use the corresponding [^\w]
negated character class and add a \p{M}
Unicode property (combining marks) there.
See more on Unicode properties here.
Upvotes: 2
Reputation: 1167
you can use filter_var
filter_var('your string &% * _',FILTER_SANITIZE_STRING | FILTER_FLAG_STRIP_HIGH);
or if you can smal symbol you can use str_replace
$arrayRequer = array('*','_','^','%');
str_replace($arrayRequer,'',$yourString);
Upvotes: 0
Reputation: 91518
Use unicode properties:
$str = preg_replace("/[^\p{L}\p{N}\p{Z}_]/u", '', 'कपिल शर्मा % * _');
Where
\p{L}
stands for any letter in any language \p{N}
stands for any digit in any language \p{Z}
stands for any kind of separator.u
flag for unicodeUpvotes: 2