user1532948
user1532948

Reputation:

retrieve all words after specific word on the line

I'm trying to retrieve just Server: Microsoft-IIS/7.5 from this three lines what is tje code please ? it an array ! i use print_r to get this result

Content-Type: text/html; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5

the code will search the word server and get all words after that just in the same line

Upvotes: 0

Views: 155

Answers (2)

Samuel
Samuel

Reputation: 17171

If that string is in the variable named $header,

preg_match("/^Server: (.*)$/m", $header, $matches);
$server = $matches[1];

Should get you the server name

Code if header lines are in an array:

$server = NULL;
for ($i = 0; $i < count($header); $i++)
{
    // Look in the line for the server header
    $is_match = preg_match("/^Server: (.*)$/", $header[$i], $matches);
    // $is_match holds the number of times our pattern matched (up to 1)
    if ($is_match > 0)
    {
        $server = $matches[1];
    }
}

Upvotes: 3

Palladium
Palladium

Reputation: 3763

If the Server: ... line is always the last line, you can use strstr.

Upvotes: 0

Related Questions