Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Spencer's avatar

Middleware to check incoming charset and convert to another

I don't know if this is even a good idea, but I need to try something. I know that the charset is in the request header and should be able to tell me what encoding the text being sent is in. But sometimes the charset isn't in the Content type of the header. Is there any consistent way to get the charset? And is it safe to assume that it is always correct? If the answer is yes to both of those questions I assume I can use built in php functions to convert from one encoding to another. So that via a middleware I can be sure that all incoming requests use the same encoding in the end.

0 likes
1 reply
bobbybouwmann's avatar

Well there is a method to determine the charset in PHP. It's called `mb_detect_encoding.

mb_detect_encoding: http://php.net/manual/en/function.mb-detect-encoding.php

To answer your question, yes you can determine the encoding. Can you be sure? Well not really because one bit of content can be valid for multiple encoding standards. So which one would you pick?

In general I don't think you should alter the request using middlewares. Instead I would enforce the one that is posting to your application to use the correct content type. This concept is what we call "content negotiation". This is what you use in your API when you tell it to always return application/json to make sure you can handle it as well. So for example when I do a request to the server and I see my content type is application/xml the server should determine if they want/can to handle that or not. If not it should return an error/exception or.

Does this help you any further?

1 like

Please or to participate in this conversation.