Archive for the ‘ PHP Interview Questions ’ Category

<?php echo getAddress(); function getAddress(){ ### check HTTPS $protocol = $_SERVER['HTTPS'] == ‘on’ ? ‘https’ : ‘http’; ### Get Full URL return $protocol.’://’.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; } ?>

<?php ### Write your email address ### $email = ‘example@imjan.com’; if(validateEmail($email) == false){ echo ‘Email Address is Invalid’; } else { echo ‘Email Address is Valid’; } function validateEmail($email){ $pattern = "/^\S+@[\w\d.-]{2,}\.[\w]{2,6}\z/iU"; return (bool)preg_match($pattern, $email, $matches); } ?>

<?php echo word_limiter(‘this is my text for t’,3); // display 3 words function word_limiter($str, $limit = 10, $end_char = ‘&#8230;’) { if (trim($str) == ”) return $str; preg_match(‘/\s*(?:\S*\s*){‘. (int) $limit .’}/’, $str, $matches); if (strlen($matches[0]) == strlen($str)) $end_char = ”; return rtrim($matches[0]) . $end_char; } ?>

Example 1: function getFileExtension($filename) { $fileinfo= pathinfo($filename); return $fileinfo[extension]; } Example 2: $ext = end(explode(‘.’, $file)); $ext = substr(strrchr($file, ‘.’), 1); $ext = substr($file, strrpos($file, .) + 1); $ext = preg_replace(/^.*\.([^.]+)$/D, $1, $file); $exts = split("[/\\.]", $file); $n = count($exts)-1; $ext = $exts[$n];

<?php echo limit_text(‘this is my text for t’, 2); //display 2 words function limit_text($text, $limit) { if (strlen($text) > $limit) { $words = str_word_count($text, 2); $pos = array_keys($words); $text = substr($text, 0, $pos[$limit]) . ‘…’; } return $text; } ?>

<?php echo wordLimiter(‘this is my text for t’,2); function wordLimiter($text,$limit=20){ $explode = explode(‘ ‘,$text); $string = ”; $dots = ‘…’; if(count($explode) <= $limit){ $dots = ”; } for($i=0;$i<$limit;$i++){ $string .= $explode[$i]." "; } return $string.$dots; } ?>

<?php $text = "<p>This is ‘a’ my first paragraph.</p><!– Comments –> sample text text"; echo "String Lenght of HTML chars:".strlen_noHtml($text); // 48 chars function strlen_noHtml($string){ $crs = 0; $charlen = 0; $len = strlen($string); while($crs < $len) { $offset = $crs; $crs = strpos($string, "<", $offset); if($crs === false) { $crs = $len; $charlen += $crs – $offset; } else { $charlen += …

$text = "<p>This is ‘a’ my first paragraph.</p><!– Comments –> sample text text"; echo "String Lenght of HTML chars:".strlen_html($text); // 24 chars function strlen_Html($string){ $crs = 0; $charlen = 0; $len = strlen($string); while($crs < $len) { $scrs = strpos($string, "<", $crs); if($scrs === false) { $crs = $len; } else { $crs = strpos($string, ">", $scrs)+1; if($crs === false) $crs = $len; …

Simply write single line of code and place the file on your Hosting server and access it through browser. <?php echo phpinfo(); ?> You will find a lot of information about your Hosting Provider Machine. Enjoy

ini_set("memory_limit", "16M");