Subscribe Newsletter

Your Name  : 
Email Address *  : 
  • MDunkleSr: Do you know of a way to get the full post variable using JInput? And...
  • brijmohankaradia: @Awkens @Darkblack add below line before while:...
  • brijmohankaradia: @Awkens @Darkblack add below line before while:...
  • Ahsan: Dear Bala-Krishna, I Was installing CATEGORY SEO META TAGS Plugin in my...
  • PNR Enquiry: Thanks for sharing all these website but you need one more i.e...
  • Train PNR Status: I usually use ww.pnrstatuss.in for checking Railway PNR Status...
  • Online Pnr Status: Before moving on let me tell you that your article was really...
  • Irctc Pnr Check: First of all, thanks for sharing such a valuable piece of...
  • Berita Terbaru: Thanks for tips! helpful..
  • pnrworld: Just by one click now a days passengers can check there current status...
  • remote backup service: Disaster recovery plan with certain objectives like backup...
  • ??? ?????? 2013: Microsoft Extended Windows 7 Enterprise 90-day Trial Download...
  • Indian Railway PNR Status Check: We can check PNR status online, via SMS and even...
  • Archives

  • Blog Stats

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.

[sourcecode language='php']
function just_clean($string)
{
// Replace other special chars
$specialCharacters = array(
‘#’ => ”,
‘$’ => ”,
‘%’ => ”,
‘&’ => ”,
‘@’ => ”,
‘.’ => ”,
‘€’ => ”,
‘+’ => ”,
‘=’ => ”,
‘§’ => ”,
‘\\’ => ”,
‘/’ => ”,
);

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;
}
[/sourcecode]


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.

AddThis Social Bookmark Button

30 Responses to “How To Clean Special Characters From PHP String”

  1. [...] How To Clean Special Characters From PHP String [...]

  2. 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!

  3. 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

  4. Hi

    Thanks for making the changes.

    It looks like on line 17 still poses as a problem?

  5. Yes, you can remove . Thanks

  6. Awkens Ryder Says:

    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"

  7. Awkens Ryder Says:

    PS: WordPress ate what I said between the first quotes.
    There was suposed to be there:

    LowerThan /code GreaterThan

  8. Thank you, this was very useful for turning strings into valid filenames.

  9. Thank you very much!! This code snippet helped me.

  10. Thanks a lot! This has solved my problem.
    What could I say “Bala Krishna ki jai”

  11. This is very useful

    thank you for share

  12. Very useful information.But whenever you use to explain codes, provide necessary examples.

  13. useful code ……

  14. thanks! i’m looking for this. when i scrape i always get those weird characters.

  15. Thanks for sharing, some codes showing error while saving.

  16. Just to say that I had problems cut&pasting the code from Firefox – it messed up the special characters – but when I viewed the page in Chrome it displayed fine.

  17. here’s an array or error to correct letter i’ve started collecting.
    i messed up with my character encoding into mySql database and had to figure out the correlation.
    hope this saves someone else the time.

    $weirdArray=array(array(“ü”,”ü”), array(“ö”,”ö”), array(“ø”,”ø”), array(“é”,”é”), array(“ß”,”ss”), array(“Ã¥”, “å”) , array(“ä”, “ä”), array(“æ”, “æ”), array(“Ã…”, “Å”), array(“Ä”, “Ä”), array(“Æ”, “Æ”), array(“æ”,”æ”), array(“é”,”é”), array(“ø”,”ø”), array(“Æ”,”Æ”), array(“á”,”á”), array(“è”,”è”), array(“ı”,”?”), array(“Ö”, “Ö”), array(“Ø”,”Ø”), array(“ô”, “”), array(“”, “”), array(“ó”, “ó”), array(“ð”, “ð”), array(“º “, “”), array(“’”, “”), array(“Â’”, “”));

  18. idk why my quotations just went all wacky

  19. I need to remove/replace all special language charakters, to store file safely.
    I tried this code for testing, but it doesn’t work.
    Where could be the problem?

    $name = $_FILES['file']['name'];
    $name = mb_convert_case($name, MB_CASE_LOWER, “UTF-8″);
    $name = strtr($name, “áä??é?í???óôš?ú?ýž”, “aacdeeillnoostuuyz”);

    Example of what this code does:
    from: TÉŠ?OVACÍ SÚBOR ?
    to: tsn?a??ovacst ssubor yz
    It should be:
    to: testovaci subor l

  20. Code has problem with character “\”.

  21. @Boys Arab

    You have to unescape it with another slash.

    It should be ‘\\’

  22. Thank you very much.I got my problem solved with this function.

  23. trim() PHP function can clean string too.

  24. @boys arab
    you can use
    preg_replace(‘/[^\x{0600}-\x{06FF}A-Za-z !@#$%^&*()]/u’,”, $text )

    to clean text with arabic chars
    thanks

  25. please remove the special charecter from my mailbox

  26. remove the special charecter from my mail box

  27. Remove all non-ascii characters in one line
    $string = preg_replace(‘/[^(\x20-\x7F)]*/’,”, $string);
    ta-da

  28. [...] How To Clean Special Characters From PHP String [...]

  29. brijmohankaradia Says:

    @Awkens @Darkblack

    add below line before while:
    array_push($specialCharacters,array(‘ ‘=>’*',));

    Thanks
    Brijmohan

  30. brijmohankaradia Says:

    @Awkens @Darkblack

    add below line before while:
    $specialCharacters[' '] = ‘*’;

    Thanks
    Brijmohan

Leave a Reply