Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How can I stop PHP from mangling diacritics?

Featured Replies

At the moment, I have to disallow diacritical characters in all of my forms because PHP does not correctly handle multi-byte Unicode characters. This is not very user-friendly, particularly if one of your clients has a lot of customers in Scandinavia... Forcing users to enter letters without diacritics is basically forcing them to misspell words. I have read hints of three possible workarounds: wait for PHP6 (not ideal); write a regex that can replace Unicode characters with ASCII; or use a function called MBstring.

 

Of the last two, which is most suitable and how would I do it? Or is there a still better solution?

  • Author

I didn't think that this would be that hard to answer. This must be an issue that other developers have encountered.

Your speaking Scandinavian mate which is not a language i know, sorry :s. :)

  • Author

Your speaking Scandinavian mate which is not a language i know, sorry :s. :)

 

Thanks for the not-at-all-helpful comment. Incidentally, you do know that Scandinavian is not a language but a region comprising four countries, right?

 

It is not as if diacritics are uncommon, since they are used in German, French, Spanish, Portuguese, even English... so surely this must be a common enough problem and somebody knows what the best solution is. Blocking all diacritics from form data is currently my only way of preventing errors in the output, but it is not helpful to users, so I need an alternative.

Hi Ade

 

Sorry for the late response I've been out of action for a while.

 

Looking at it I'd use mb_convert_encoding(), example below:

 

<?php echo mb_convert_encoding('ÖÄÜöäü', "UTF-8"); ?>

Let me know if this helps you :)

 

BenTheDesigner

  • 2 weeks later...

I'd recommend storing it all as UTF-8 as that way you're able to handle all languages.

 

First of all add a UTF-8 header to your HTML file.

 

Secondly you need to make sure if you're storing the data in a database that you have the encoding of that field set to UTF-8 Unicode.

 

You then should look at the Multibyte functions built into default installations of PHP.

http://www.php.net/manual/en/ref.mbstring.php

 

 

To help you in making sure your script is utf-8 friendly i'll need to see it, however, the above should be enough to sort you out.

  • Author

I'd recommend storing it all as UTF-8 as that way you're able to handle all languages.

 

First of all add a UTF-8 header to your HTML file.

 

Secondly you need to make sure if you're storing the data in a database that you have the encoding of that field set to UTF-8 Unicode.

 

You then should look at the Multibyte functions built into default installations of PHP.

http://www.php.net/manual/en/ref.mbstring.php

 

 

To help you in making sure your script is utf-8 friendly i'll need to see it, however, the above should be enough to sort you out.

 

Thanks for replying, but I rarely need to deal with databases. I leave that to MySQL specialists and stick to my own specialisms. This is about e-mail output. Including a UTF-8 dec is "web 101" and has no effect here anyway. If it did, I would not have this issue.

Thanks for replying, but I rarely need to deal with databases. I leave that to MySQL specialists and stick to my own specialisms. This is about e-mail output. Including a UTF-8 dec is "web 101" and has no effect here anyway. If it did, I would not have this issue.

 

 

Sorry didn't read your email correctly.

 

If you use mb_send_mail your problem should be solved.

 

http://uk2.php.net/manual/en/function.mb-send-mail.php

  • Author

Ben, Kieran - not quite solved. I looked briefly at the various mb functions but they are not well explained in the PHP manual (like the rest of its content). As I have a lot of work on, I may have to leave this unresolved and outsource it at a later date.

  • 2 weeks later...
  • Author

This seems to be fully sorted now. I added a header to set the content type to utf-8 (by default, the e-mails were being encoded in 8891-1) and that has preserved all special characters correctly. A simple solution, after all!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.