Remove special HTML characters from string in PHP -
this question has answer here:
- php remove special character string 7 answers
i found many results reason nothing works me! i've tried preg_replace
regex , html_entity_decode
, no good...
i want select words has hash mark prefix e.g. #word
, works fine, hash mark read ‏#word
, misses up.
example: this normal #hash_mark #this_isn't
as appears:
the regex use select words hash mark prefix '~(?<=\s|^)#[^\s#]++~um'
in question marked duplicate, answer doesn't work unicode text, seen in image:
the code remove special characters including unicode text, what's required replace ‏#
normal #
function remove_special_char($sentence){ return preg_replace('/[^a-za-z0-9_ %\[\]\.\(\)%&-]/s','',$sentence); } echo remove_special_char("hello مرحبا привет שלום");
output:
hello
there 2 different characters special_characters
let happened i've made debug
var_dump(ord('#')); //return ascii value of char $str1 = 'this character 226 #'; $str1v1 = preg_replace('/[^a-za-z0-9_ %\[\]\.\(\)%&-]/s', '', $str1); var_dump(ord('#')); //return ascii value of second char $str2 = "this character 35 #"; $str2v1 = preg_replace('/[^a-za-z0-9_ %\[\]\.\(\)%&-]/s', '', $str2); var_dump($str1v1); var_dump($str2v1); var_dump($str1); var_dump($str2);
output:
int 226 int 35 string 'this character 226 ' (length=22) string 'this character 35 ' (length=21) string 'this character 226 â€#' (length=26) string 'this character 35 #' (length=22)
maybe or end user have done copy , paste somewhere , included converted charcode described (‏#)
. since rendered same surface , make confused.
to escape characters, have used regex in following line
preg_replace('/[^a-za-z0-9_ %\[\]\.\(\)%&-]/s', '', $str1);
the regex has been taken php remove special character string
Comments
Post a Comment