I have an string like this:
<div class="myclass">
ما قصد داریم تنها کاراکترهای فارسی را خارج کنیم. اما مشکلاتی پیش رو داریم, از جمله کاراکترهای خاص. آیا راهی هست؟ لطفا راهنمایی کنید.
</div>
now I want my output be like this one:
ما قصد داریم تنها کاراکترهای فارسی را خارج کنیم اما مشکلاتی پیش رو داریم از جمله کاراکترهای خاص آیا راهی هست لطفا راهنمایی کنید
i can't use str_replace
or preg_replace
because some time there is some character like �
in my output
You probably need to use the Multibyte String Functions.
See: http://php.net/manual/en/ref.mbstring.php
Your Persian string uses multibyte character encoding, and str_replace() cannot handle that correctly (unless it is UTF-8 see comment below).
There are more issues:
1 Make sure you store the PHP file in the correct character encoding on the server (tip by Mark).
2 Also make sure you send the correct header in your HTML output, for instance:
<head>
<meta charset="UTF-8">
</head>
when using UTF8. And also do it in PHP:
header('Content-Type: text/html; charset=utf-8');
Everything must be correct, or it will not work. Dealing with character encoding can be quite difficult. Have you seen the mb_detect_encoding()
function? Use this on your original string to learn its encoding.