Useful PHP Functions

Useful PHP Functions

Web Stuff 01.08.2016 2115

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);

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


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() {
        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.

Comment(s) (8)

  • avatar syarat

    Hey! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I'm getting fed up of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform.I would be awesome if you could point me in the direction of a good platform.

    01.09.2016 1 Reply
  • avatar Simon

    Hey! I just wanted to ask if you ever have any trouble with hackers?My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any solutions to prevent hackers?

    21.09.2016 1 Reply
    • avatar Jerome

      With WordPress and any other software you should always update to the latest version. There is a reason why new versions are released, it is not just for more features usually developers also work in security improvement and bug fixing.

      23.09.2016 1
  • avatar Thomas

    Hallo, Ich finde den Aufbau der Seite klasse. Macht weiter so.

    19.10.2016 1 Reply
  • avatar 性感女同性恋

    Hurrah, that's what I was looking for, what a stuff!present here at this blog, thanks admin of this web page.

    29.10.2016 1 Reply
  • avatar dramade

    I think this is among the most important information for me. And i am glad reading your article.

    12.02.2017 1 Reply
  • avatar plugo
    Great post. Keep writing such kind of information on your blog. Im really impressed by your site.
    25.01.2018 0 Reply

Hey, leave a comment...