try this .. works for me.

iconv('utf-8', 'ascii//TRANSLIT', $text);
Answer from Stewie on Stack Overflow
🌐
PHP
php.net › manual › en › function.str-replace.php
PHP: str_replace - Manual
Instead, use preg_replace: <?php $challenge = '-aaa----b-c-----d--e---f'; echo str_replace('--', '-', $challenge).'<br>'; echo preg_replace('/--+/', '-', $challenge).'<br>'; ?> This outputs the following: -aaa--b-c---d-e--f -aaa-b-c-d-e-f ... ...
🌐
Learn-codes
learn-codes.net › php › replace-special-characters-with-normal-characters-in-php
Replace special characters with normal characters in php | Learn-codes.net
<?php $code="CSC113α"; $full_course_id = str_replace(array('α','β', 'δ'), array('A', 'B', 'D'), $code); str_replace( $searchVal, $replaceVal, $subjectVal, $count ) ... $string = "Wel%come *to( codex<world, the |world o^f pro?gramm&ing.";// Remove special characters$cleanStr = preg_replace('/[^A-Za-z0-9]/', '', $string); ... function cleanStr($string){ // Replaces all spaces with hyphens.
Top answer
1 of 2
5

There's a great tip from this page: How to remove diacritics from text? Here's my version of it:

/** Normalize a string so that it can be compared with others without being too fussy.
*   e.g. "Ádrèñålînë" would return "adrenaline"
*   Note: Some letters are converted into more than one letter, 
*   e.g. "ß" becomes "sz", or "æ" becomes "ae"
*/
function normalize_string($string) {
    // remove whitespace, leaving only a single space between words. 
    $string = preg_replace('/\s+/', ' ', $string);
    // flick diacritics off of their letters
    $string = preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);~i', '$1', htmlentities($string, ENT_COMPAT, 'UTF-8'));  
    // lower case
    $string = strtolower($string);
    return $string;
}

It's good because, unlike the iconv method mentioned above, there's no converting between character sets (they're a minefield).

2 of 2
3

I copied and pasted your code into my editor and something interesting happened. Instead of getting adios I was getting adjiós. Notice the j in the middle after the d. This was coming from the 'đ'=>'dj', in the first line of the table map. Apparently, my editor changed the đ to a regular d, and then it wouldn't convert the ó. I removed this key/value pair and suddenly it worked for me. Are you sure all of your keys are correct in your editor (Does you editor accept alternative character sets?) Here is my test file (with the đ removed:

<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
</head>
<body>
<?php

function normalize ($string) {
    $table = array(
        'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj', 'Ž'=>'Z', 'ž'=>'z', 'C'=>'C', 'c'=>'c', 'C'=>'C', 'c'=>'c',
        'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
        'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
        'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
        'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
        'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
        'ÿ'=>'y', 'R'=>'R', 'r'=>'r',
    );

    return strtr($string, $table);
}

$word = 'adiós';
$length = strlen($word);

echo 'original: '. $word;
echo '<br />';
echo 'normalized: '. normalize($word); 
echo '<br />';
echo 'loop: ';

for($i = 0; $i < $length; $i++) {
    echo normalize($word[$i]);
}

?>

</body>
</html>

When I loop through each character with the 'd' => 'dj' in the array map then I correctly get adjios

🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-remove-special-character-from-string-in-php
How to Remove Special Character from String in PHP? - GeeksforGeeks
September 10, 2024 - The str_ireplace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (" "). The difference between str_replace and str_ireplace is that str_ireplace is case-insensitive. ...
🌐
sebhastian
sebhastian.com › php-replace-special-characters
How to replace special characters in a string with PHP | sebhastian
November 30, 2022 - <?php $str = "john*spider*wick"; $new = str_replace("*", "&", $str); print $new; // Output: john&spider&wick · You can also replace a special character with a white space.
🌐
Facetwp
gist.facetwp.com › gist › gist-0786f9f60afd6e5796da144251ec2e17
Replace special characters with “normal” ASCII characters during indexing – Gists – FacetWP
<?php // Add the following to your theme's functions.php add_filter( 'facetwp_index_row', function( $params, $class ) { if ( 'aufsichtsbehoerden' == $params['facet_name'] ) { $raw_value = $params['facet_value']; $params['facet_value'] = iconv( 'UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $raw_value ); } return $params; }, 10, 2 );
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › php › remove special characters from string php
How to Remove Special Character in PHP | Delft Stack
February 2, 2024 - str_replace() is also helps to replace that character with the removed character. This function contains few parameters, as introduced below. $search_str: It contains such value which we want to search in the given string. $replace_str: It stores a value that you want to replace, or you can also leave it empty if you only want the removal of a special character. ... See the example code. <?php $mainstr = "This is a sim'ple text;"; echo "Text before remove: \n" .
🌐
W3Schools
w3schools.com › php › func_string_str_replace.asp
PHP str_replace() Function
❮ PHP String Reference · Replace ... ?> Try it Yourself » · Share Link Copied · The str_replace() function replaces some characters with some other characters in a string....
🌐
Michael Jacobsen
michaeljacobsen.ninja › resources › article › replace-special-characters-with-their-html-entity
Replace Special Characters with their HTML Entity (PHP) - Michael Jacobsen | Web Developer, Web Designer, Geek
April 4, 2017 - <?php //################################################################# // CHARACTER TO HTML ENTITY //################################################################# function characterToHTMLEntity($str) { $search = array('&', '<', '>', '€', '‘', '’', '“', '”', '–', '—', '¡', '¢','£', '¤', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '®', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ',
🌐
PHP Dev Tips
phpdevtips.com › 2011 › 08 › using-php-to-replace-special-characters-with-their-equivalents
Using PHP to Replace Special Characters with their Equivalents - PHP Dev Tips PHP Dev Tips
August 25, 2011 - We introduce four principles to ... pages: 59-67<br />Evaluation of New York&acirc;s driver improvement program"; echo normalize_str($text); Tags: character replacement, data conversion, equivalents, microsoft word, normalization, ...
🌐
DevBest.com
devbest.com › forums › software development › programming › programming q&a
PHP special characters replace option | DevBest.com - Community of Developers & Gamers
March 4, 2020 - <?php phpinfo(); ?> This will output info about your PHP instance. Look for "Loaded Configuration File". Open the file at that location, and find the setting default_encoding—the setting may be commented out with a semi-colon. Just replace the whole line with the following:
🌐
GitHub
gist.github.com › swas › 10643194
PHP replacing special characters like à->a, è->e · GitHub
PHP replacing special characters like à->a, è->e · Raw · gistfile1.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
StackHowTo
stackhowto.com › home › php › how to remove special characters from a string in php
How to remove special characters from a string in PHP - StackHowTo
October 12, 2021 - <?php function deleteSpecialChar($str) { // replace all special characters by empty string $res = str_replace( array( '%', '@', '\'', ';', '<', '>' ), ' ', $str); return $res; } // string example $str = "A % B @ C <D>'E;"; // Call the function ...
🌐
DEV Community
dev.to › bdelespierre › convert-accentuated-character-to-their-ascii-equivalent-in-php-3kf1
Convert accentuated character to their ASCII equivalent in PHP - DEV Community
August 6, 2020 - * * @param string $str * @param string $charset * @return string */ function accent2ascii(string $str, string $charset = 'utf-8'): string { $str = htmlentities($str, ENT_NOQUOTES, $charset); $str = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str); $str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); // pour les ligatures e.g. '&oelig;' $str = preg_replace('#&[^;]+;#', '', $str); // supprime les autres caractères return $str; } Don't forget to leave a like to encourage me to post more useful PHP snippets.