for example, if you type in a browser

https://yandex.ru/search/?text= hi

the browser will make a request to the server

https://yandex.ru/search/?text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82

Need to do the same function.

Directly using Urlencode is not appropriate, because you need to code selectively, i.e. Only Russian letters and other utf characters, and all % & / leave as is.

It is clear that you can write yourself, but maybe some kind of library is available for this?

  • 2
    Beat in the same Yandex% and & and you will understand that you should not leave them as they are :) - andreymal
  • In the browser, not in Yandex. Yandex has nothing to do with it - varan
  • and if you type a Cyrillic domain in the address bar, you will generally go crazy. - Evgeny Borisov
  • And you drive it in Yandex and see what happens :) - andreymal
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

There is no simple solution. As a starting point, you can manually parse the component parts. In your example, this option will give a ride, but catch errors when the parts are not announced. For example, I now had a break in checking for the presence of the path and request parameters.

 / ** rough draft solution * / function unsafe_url_encode ($ url) {$ url_parts = parse_url ($ url);  / ** split url into parts * / $ path = preg_split ('# / #', $ url_parts ['path']);  / ** split the path into parts * / $ path = array_map ('urlencode', $ path);  / * encode each part of the path * / $ url_parts ['path'] = implode ('/', $ path);  $ args = preg_split ('# & #', $ url_parts ['query']);  / ** split parameters into parts, and process them * / foreach ($ args as $ k => $ v) {$ argument = preg_split ('# = #', $ v);  $ argument = array_map ('urlencode', $ argument);  $ args [$ k] = implode ('=', $ argument);  } / ** put everything in a heap * / $ args = implode ('&', $ args);  $ url_parts ['query'] = $ args;  extract ($ url_parts);  $ retval = "$ {scheme}: // $ {host} $ {path}";  if (strlen ($ query)) $ retval. = "? $ {query}";  if (isset ($ fragment)) $ retval. = '#'. urlencode ($ fragment);  return $ retval;  } echo unsafe_url_encode ("https://yandex.ru/search/?text= hello"); 

An online sandbox with an example: http://sandbox.onlinephpfunctions.com/code/fd073f99c4a76f08ec34aef44e810ce4aafdaf47