JohnA
JohnA

Reputation: 1913

How to convert special characters entered in form into similar English characters before placing in database?

e.g. we have form that ask for first name last name country, country is drop down list, but name is free entry. We need to make sure we have no special characters in input field. And instead convert them to english. e.g.

Á -> A,á -> a,Č -> C,č -> c,ď -> d,é -> e,ě -> e,É -> E,Ě -> E,í -> i,Í
-> i,Ň -> N,ň -> n,Ó -> O,ó -> o,Ř -> R,ř -> r,Š -> S,š -> s,ť -> t,Ú -> U,ú -> u,Ů -> U,ů -> u,
Ý -> Y,ý -> y,Ž -> Z,ž -> z

Is there some way to do it in php or mysql?

Upvotes: 1

Views: 586

Answers (2)

andyb
andyb

Reputation: 43823

If you can use PHP >= 5.3 then normalizer.normalize() should do the job. By the way, the name for what you want to do is removing diacritics. If you are using an earlier version of PHP then you should be able to use the same function if you enable the intl PHP extension (as commented here How to remove diacritics from text?)

Edit: Found a related question How do I remove accents from characters in a PHP string?

Upvotes: 0

Gundars Mēness
Gundars Mēness

Reputation: 508

Use php function utf8_encode

Or detect encoding and change it if it is not utf-8, like below:

<?php
//$s is a string from whatever source
mb_detect_encoding($s, "UTF-8") == "UTF-8" ? : $s = utf8_encode($s);
?>

Upvotes: 1

Related Questions