Sunday 24 September 2017

Remove non printable and non UTF8 characters url encoded json string in PHP/C#

I have been working on an API (Invitation) which is used to invite external users to a web application. According to invitation API, user gets an invitation where he receives an email with a hyperlink for setup the account and confirmation as shown below.

Invitation Email

The above Get started link contains some necessary details of the invited user which is encrypted and encoded. Once the link is clicked it will redirect to another page where all the decode and decryption process will be done. Sample url encoded json is shown below

https://testdemo.com/entry/Form.html?lPz2ijUnk5wmYMzUYdB40XJie0lkntq9aloNbpyF%2Bp9Hb2FIpMW1aQb9CO9avq0BE5YNjeDuU%2B

Now let's come to decode process. Most of us have experienced with a tricky problem when it comes to json decode. Once the decryption is done u will see there are some non printable characters appended with json which won't allow json string to be decoded.

json with special characters

In order to fix this issue in PHP, once the decryption is done we can remove the special characters using below code.

Code for removing special characters
Once we remove the special characters now we can decode without any problem. Likewise, we can remove the special characters in C# using below code.

string output = Regex.Replace(input, @"[^\u0009^\u000A^\u000D^\u0020-\u007E]", "*");
  • ^\u0009 means if not TAB
  • ^\u000A means if not Line Feed
  • ^\u000D means if not Carriage Return
  • ^\u0020-\u007E means if not fall into space to ~
refer ascii table if you like to make changes, remember it would strip off each non ascii character.

No comments:

Post a Comment

Integrating SonarQube with Jenkins for PHP

SonarQube  is a quality management tool popularly used among 85000 companies around the world. It has four editions. Community Edition ( ...