Wednesday, 7 October 2015

Regular Expression Interview Questions and Answers


Regular Expression Interview Questions and Answers


Question: What is the mean of Regular Expressions?
Regular Expressions is a pattern which is used to search string like "Search name start with a" OR "Search name end with z".


Question: What is use of Regular Expressions?
It is used to Search Simple String OR Complex String.
For Example:
Search all name start with a.
Search all names end with a


Question:What the mean of different symbols like ^ $ in Regular Expression?
^a Search all string which start with "a".
a$ Search all string which end with "a".
[a-z0-9]{2,5} there should be 2-5 string which can be a-z and 0-9 characters.
[a-z]{2,5} there should be 2-5 string which are a-z characters.
[0-9]{2,5} there should be 2-5 string which are 0-9 characters.


Question: What is Regular Expression to validate Email?
^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$


Question: Write a Regular expression to match line that doesn't contain a word?
To find a word "FINDME" in String.
^((?!FINDME).)*$


Question: How do I remove all non alphanumeric characters from a string?
Suppose you want to remove all character except a-z, 0-9 and "space".
[^a-zA-Z0-9\s] 


Question: Simple regular expression for a decimal with a precision of 2
\d+(\.\d{1,2})?


Tuesday, 6 October 2015

How can I read request-headers in PHP


$headers = apache_request_headers();
print_r($headers);

Following are the Response of apache_request_headers()
How can I read request-headers in PHP?



Question: What is apache_request_headers()?
This function fetch all HTTP request headers.


Question: What format it return when success?
An associative array of all the HTTP headers in the current request.


Question: What happen when request failed?
It return FALSE when failed.


Question: Is it availble in all PHP Version?
No, It works only with >= PHP 5.4.


Question: How can I get header values less than 5.4 version
function getRequestHeadersCustomFunction() {
    $headers = array();
    foreach($_SERVER as $key => $value) {
        if (substr($key, 0, 5) <> 'HTTP_') {
            continue;
        }
        $headerKey = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
        $headers[$headerKey] = $value;
    }
    return $headers;
}
print_r(getRequestHeadersCustomFunction());