How To Clean Special Characters From PHP String
If you are looking for the PHP special characters clean function then this post might be useful for you. This function can used to remove special character as well as able to replace specific character with other equivalent character or string.
Here is detailed explanation of function:
$specialCharacters Array: This array used to replace special character with other character or string. if you want to interchange (+) with (plus) then you need to specify in the array. You can add/remove array element according to your requirement.
strtr function: This function used to replace other language special characters to plain English character. You can remove this line safely but ensure before that these characters not in your string and you don’t need to remove them.
Last 4 line used to remove other unknown unwanted special characters.
function just_clean($string)
{
// Replace other special chars
$specialCharacters = array(
'#' => '',
'$' => '',
'%' => '',
'&' => '',
'@' => '',
'.' => '',
'' => '',
'+' => '',
'=' => '',
'' => '',
'\' => '',
'/' => '',
);</code>
while (list($character, $replacement) = each($specialCharacters)) {
$string = str_replace($character, '-' . $replacement . '-', $string);
}
$string = strtr($string,
"? ",
"AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn"
);
// Remove all remaining other unknown characters
$string = preg_replace('/[^a-zA-Z0-9-]/', ' ', $string);
$string = preg_replace('/^[-]+/', '', $string);
$string = preg_replace('/[-]+$/', '', $string);
$string = preg_replace('/[-]{2,}/', ' ', $string);
return $string;
}
Read Related Post
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.



June 26th, 2008 at 9:13 am
[...] How To Clean Special Characters From PHP String [...]
December 18th, 2008 at 5:04 am
Thanks for this post.
Can you send me the full source code in plain text document (Notepad)? The code is malformed when I copy and paste from this post.
Thanks!
December 18th, 2008 at 5:39 am
Thanks to inform me for malformed code. I have corrected formatting issue. You can now copy code to clip board easily. Let me know if you still have trouble copying code.
Thanks
December 18th, 2008 at 11:34 pm
Hi
Thanks for making the changes.
It looks like on line 17 still poses as a problem?
December 19th, 2008 at 2:17 am
Yes, you can remove . Thanks
February 27th, 2009 at 7:52 am
It looks like on line 17 the website didn’t filter the post code since it has some raw HTML on it. “"
Removing it solved the problem but now one persists.
I entered: "Água do Lúso" and the returned string was: "n guaOdoOLn so"
February 27th, 2009 at 7:54 am
PS: WordPress ate what I said between the first quotes.
There was suposed to be there:
LowerThan /code GreaterThan
May 4th, 2009 at 2:41 am
Thank you, this was very useful for turning strings into valid filenames.
August 14th, 2009 at 1:22 am
Thank you very much!! This code snippet helped me.
August 20th, 2009 at 10:48 pm
Thanks a lot! This has solved my problem.
What could I say “Bala Krishna ki jai”
September 9th, 2009 at 7:57 pm
This is very useful
thank you for share
April 15th, 2010 at 8:00 pm
Very useful information.But whenever you use to explain codes, provide necessary examples.
May 24th, 2010 at 8:19 pm
useful code ……
June 30th, 2010 at 6:37 pm
thanks! i’m looking for this. when i scrape i always get those weird characters.