It is necessary to delete everything from the "HTTP / 1.1 400 Bad Request" line except the response code (400)

2 answers 2

Alternatively, you can simply cut out the HTTP response code and replace it with the original string

<?php $str = "HTTP/1.1 400 Bad Request"; preg_match("/\d{3}/", $str, $out); $str = $out[0]; // 400 

    Open the HTTP protocol description and find

    Status line

    It has been noted that it has been the SPT line. No CR or LF is allowed except in the final CRLF sequence.

      Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF 

    It's simple. The numeric status code is in the second field, the fields are separated from each other by a space.

     $statusLine = "HTTP/1.1 400 Bad Request"; preg_match("/^(\S+)\s+(\d+)\s+(.*)/", $statusLine, $matches); $httpVersion = $matches[1]; $statusCode = $matches[2]; // здесь ваш код $reasonPhrase = $matches[3]; 

    View a beautiful color test online https://regex101.com/r/sM9uW4/1