skip to Main Content

When I enter the string DIN 2080 Fräseraufnahmen SK 30 into the browser, it encodes it as DIN%202080%20Fr%C3%A4seraufnahmen%20SK%2030, and the API properly reads this. However, in a PHP script to call the same API, when I encode the string using the function rawurlencode(), it is encoded as DIN%202080%20Fra%CC%88seraufnahmen%20SK%2030. This causes the same API to misidentify the string as not matching. How do I encode the string in PHP so that it will be same same as encoded by the browser?

Browser:

DIN%202080%20Fr%C3%A4seraufnahmen%20SK%2030

PHP rawurlencode():

DIN%202080%20Fra%CC%88seraufnahmen%20SK%2030

3

Answers


  1. You can use the PHP Normalizer class to convert the string using the same normalization form as the browser uses. You do need to have the intl PHP extension installed for this to work though.

    This should be how this would work:

    $normalizedString = Normalizer::normalize($inputString, Normalizer::FORM_C);
    
    Login or Signup to reply.
  2. Based on my experience, it seems like the character set of the editor(vscode…) is the problem.

    Check whether the editor character set is UTF-8.

    good luck.

    enter image description here

    Login or Signup to reply.
  3. Your desired output requires replacing spaces with "%20" you can try:

    $encodedString = str_replace(" ", "%20", rawurlencode($string));
    

    does that answer your question or you want some different approach?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search