Archive for the ‘ PHP Interview Questions ’ Category
Get Full URL of User / Visiter System in PHP
on March 5, 2010
<?php echo getAddress(); function getAddress(){ ### check HTTPS $protocol = $_SERVER['HTTPS'] == ‘on’ ? ‘https’ : ‘http’; ### Get Full URL return $protocol.’://’.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; } ?>
How to Validate an Email Address By a Regular Expression in PHP?
on March 4, 2010
<?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); } ?>
Word Limiter Function that Preserves the Original Whitespace
on March 3, 2010
<?php echo word_limiter(‘this is my text for t’,3); // display 3 words function word_limiter($str, $limit = 10, $end_char = ‘…’) { 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; } ?>
How to Get File Extension Using PHP?
on March 2, 2010
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];
A Cute Little Function for Truncating Text to a Given Word Limit
on March 2, 2010
<?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; } ?>
How to Append Dots After Particular Number of Words in PHP?
on March 1, 2010
<?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; } ?>
Count HTML Tags in PHP through Strlen() Function
on February 28, 2010
<?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 += …
Count a String With Including HTML Tags through Strlen() Function
on February 27, 2010
$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; …
How to Find Hosting Server Information / Specifications?
on February 26, 2010
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
How to Increase Memory Limit in PHP (php.ini)?
on February 25, 2010
ini_set("memory_limit", "16M");

