How do i remove “
, ”
, °
like special characters from a string in PHP
Actually i need to read the contents from a text file and need to remove the html special characters except the alphabets and digits from it
How do i remove “
, ”
, °
like special characters from a string in PHP
Actually i need to read the contents from a text file and need to remove the html special characters except the alphabets and digits from it
One way would be (eg)
$str = 'a“bc”def°g';
$special = array('“','”','°');
$str = str_replace($special,'',$str);
Or, if you want to use a RegEx, use:
$str = 'a“bc”def°g';
$str = preg_replace("[^A-Za-z0-9 ]", "", $str);
Per the edit- to read the file contents, remove everything except alphanumerics and space characters then write the result to the same file, you can use:
$file= "yourfile.txt";
$file_content = file_get_contents($file);
$fh = fopen($file, 'w');
$file_content=preg_replace("[^A-Za-z0-9 ]", "", $file_content);
fwrite($fh, $file_content);
fclose($fh);