Tag Archives: PHP

How to cut the string without breaking any words

<?php function Cut($string, $max_length){ if (strlen($string) > $max_length){ $string = substr($string, 0, $max_length); $pos = strrpos($string, ” “); if($pos === false) { return substr($string, 0, $max_length).”…”; } return substr($string, 0, $pos).”…”; }else{ return $string; } } $string = ‘How to cut the string without breaking any words’; echo Cut($string,30); ?> The output is

Read more

Getting a file extension using PHP

<?php function getExt($filename){ return substr(strrchr($filename, ‘.’), 1); } $file = ‘myphoto.jpg’; echo getExt($file); ?> The output is

Read more

PHP matching string

We can match string in PHP using strrpos function here is the sample code <?php function Match($keyword,$subject){ if(strrpos($subject,$keyword)===false){ return false; }else{ return true; } } $keyword = ‘insicdesigns.com’; $subject = ‘I love insicdesigns.com’; if(Match($keyword,$subject)){ echo ‘Match!’; }else{ echo ‘Not found’; } ?> It will display…

Read more

PHP Email Validation

Email validation is very important in application development. Here I show you a sample PHP code on how to validate email address. <?php function isEmail($email){ if(!eregi(“^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”, $email)){ return false; }else{ return true; } } $email = ‘test@test’; if(!isEmail($email)){ echo ‘Invalid email address’; }else{ echo ‘Ok’; } ?> The output is…

Read more

PHP Random Numbers & letters

This is the simple function of generating a random numbers in php. <?php function Random($length=5){ $key = ”; $pattern = “12345678901234567890123456789012345678901234567890″; for($i=0;$i You can also add a different pattern like letters and combination of letters and numbers. <?php function Random2($length=5,$type=1){ $key = ”; switch($type){ case 2: $pattern = “abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz”; break; case 3: $pattern = “12345678901234567890123456789012345678901234567890″; [...]

Read more

How to get your visitor’s IP address

<?php echo ‘Your IP ‘.$_SERVER['REMOTE_ADDR']; ?>

Read more