How to Replace Special Characters With the Ones They'Re Based on in PHP

how to replace special characters with the ones they're based on in PHP?

This answer is incorrect. I didn't understand Unicode Normalization when I wrote it.
Look at francadaval's comment and link

Check out the Normalizer class to do this. The documentation is good, so I'll just link it instead of repeating things here:

http://www.php.net/manual/en/class.normalizer.php

Specifically, the normalize member of that class:

http://www.php.net/manual/en/normalizer.normalize.php

Note that Unicode normalization has several forms, and you seem to want Normalization Form KD (NFKD) Compatibility Decomposition, though you should read the documentation to make sure.

You shouldn't try to roll your own function for this: There's way too many things that can go wrong, and using the provided function is a much better idea.

Remove all special characters from a string

This should do what you're looking for:

function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

Usage:

echo clean('a|"bc!@£de^&$f g');

Will output: abcdef-g

Edit:

Hey, just a quick question, how can I prevent multiple hyphens from being next to each other? and have them replaced with just 1?

function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}

Issues replacing special characters in PHP string

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

Find and Replace special chracters in a string

You can use str_replace or preg_replace

Example preg_replace http://php.net/manual/en/function.preg-replace.php

  $str = preg_replace("/\W+/", '', $str);
var_dump($str);

Example str_replace http://php.net/manual/en/function.str-replace.php

$search =  '!"#$%&/()=?*+\'-.,;:_' ;
$search = str_split($search);
$str = "OF!" ;

var_dump(str_replace($search, "", $str));

Output

string 'OF' (length=2)

How to replace all available special character and numbers in php using regular expression

You can try this:

To remove just special characters use this.

$enc = "HyS7Nj+c3b3+1kaT6gLpK9kDQS3lIDtYUNQHtz/bLAw=";
echo preg_replace('/\W/', '', $enc);

To remove both numbers and special characters use this.

$enc = "HyS7Nj+c3b3+1kaT6g$#@LpK9kDQS3lIDtYU%^NQHtz/bLAw=";
echo preg_replace('/\W|\d/', '', $enc);

How to replace some special characters?

Here's a solution that uses three separate regular expressions to do successive transformations to the string:


<?php

$regex1 = "/[^0-9a-z\s]+/i"; // Search for everything that is not alphanumeric or whitespace
$regex2 = "/\s+/"; // Search for whitespace
$regex3 = "/-+$/"; // search for trailing dashes

$text = "Hello World - This/That is (Test)";

// replace in turn, non-alpha with space, one or more spaces with dash,
// one or more trailing dashes with nothing
$text2 = preg_replace([$regex1, $regex2, $regex3],[' ','-',''], $text);

// Switch everything to lower case
$text2 = strtolower($text2);

echo $text2; // hello-world-this-that-is-test

Demo:https://3v4l.org/Pe5hC

(The regex purists might be able to condense this further, but this is clear and easily modifiable)

replace special characters with normal characters in php

Why not simply use the function str_replace? I've done a test and it works for me!

<?php
$code="CSC113α";
$full_course_id = str_replace(array('α','β', 'δ'), array('A', 'B', 'D'), $code);


Related Topics



Leave a reply



Submit