There is a task to take the file "robots.txt" from the address specified by the user, and find in it the number of directives "Host". The file "robots.txt" for example -

User-agent: Yandex Disallow: / host: example.com host: example2.com 

I take the file from the remote server at the user's request and the character encoding can be any. In order not to be attached to the case of characters I try to do this:

 $url = 'http://tests.loc/robots.txt'; $maxsize = 32768; $ctx = stream_context_create(array('http' => array('timeout' => 10))); $directives = file_get_contents($url, false, $ctx, -1, $maxsize); setlocale(LC_ALL, "en_US.UTF-8"); echo strtolower($directives); //Возвращает ту же строку без изменения регистра символов echo substr_count($directives, 'host'); 

Tell me what's wrong? Or maybe this problem can be solved otherwise.

  • How exactly does "not work"? Returns the same string without changing the case of characters? If the file really contains non-ASCII text, then you can use mb_strtolower () - astax
  • In the editor, the {} button means inserting the code. Please use it. - Naumov
  • Naumov understood, thanks. astax, yes returns the same string without changing the case of characters. mb_strtolower () doesn't help either, mb_internal_encoding () installed. I take the file from the remote server at the user's request and the character encoding can be any after all - Rimarx

0