Useful PHP Functions

Useful PHP Functions

Web Stuff 01.08.2016 4196


Useful PHP Functions

Today we will share some really useful PHP functions that will make your life easier and faster when creating new code.

PHP functions are great to reduce and simplify your code. As soon you use a code more then once, make a class for it. Very simple and very practical.

Now let's have a few very useful PHP functions, we use it daily and in all our projects.

Redirect your visitor to another page.

function redirect($url, $code = 302) {
    header('Location: '.$url, true, $code);
    exit();
}

To use this function and redirect your visitor to another page you will only need to use:

redirect("new_page.php");

How simple is that? Plus you can use it as many times you want for all your redirection calls, but hang on why no second parameter? You can but because we set a default parameter you don't have to if you use 302 anyway.

Filter User Input

function input_filter($value) {
	$value = filter_var($value, FILTER_SANITIZE_STRING);
	return preg_replace("/[^0-9 _,.@-p{L}]/u", '', $value);
}

This small function will filter the content from input fields for example:

$filtered = $input_filter($_POST["title"];

Cut some long text

function cut_text($text,$limit,$ending) {
	
	// empty limit
	if (empty($limit)) $limit = 160;
    $text = trim($text);
    $text = strip_tags($text);
    $text = str_replace(array("r","n",'"'), "", $text);
    $txtl = strlen($text);
    if($txtl > $limit) {
        for($i=1;$text[$limit-$i]!=" ";$i++) {
            if($i == $limit) {
                return substr($text,0,$limit).$ending;
            }
        }
        $jakdata = substr($text,0,$limit-$i+1).$ending;
    } else {
    	$jakdata = $text;
    }
    return $jakdata;
}

Let's say you code a blog and you want to preview the article instead of showing the whole content, easily use the cut_text function. The function will cut to the length you set and does not cut off words, so your preview text always looks nice. You know how to use it now, don't you?!

Create a random password

// Password generator
function password_creator($length = 8) {
	return substr(md5(rand().rand()), 0, $length);
}

That should be self explained, the function will return a password with the length of your choice (8 characters are standard).

Encode email address

// encrypt email address (prevent spam)
function encode_email($e) {
	for ($i = 0; $i < strlen($e); $i++) { $output .= '&#'.ord($e[$i]).';'; }
	return $output;
}

Very useful function to prevent spam on your displayed email addresses. Simply encode your email address with this function before you display the email address.

Get IP Address

function get_ip_address() {
    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                    return $ip;
                }
            }
        }
    }
    
    return 0;
}

Last function for today is get your visitors IP address the best and most accurate way.

Now get back to work, use our functions and simplify your code. Thank you for reading another article from JAKWEB. Please feel free to post your suggestions or feedbacks below.