Archive for the ‘PHP’ Category

Encode String to Only Alphanumeric Chars

November 27th, 2014 No comments

If you need to encode a string with all sorts of problem characters into a URL-safe string, urlencode() is not right solution.

What you need is a function that will give you only numbers and letters. No %’s or &’s, spaces, or any other non-alphanumeric character.

For this we will use PHP’s bin2hex() function, and its pack() function.

For example, it can convert: “(!X>4Ob=h/&hN\'” into this much nicer string: “2821583e344f623d682f26684e5c27”.

Here are the functions you need:

function hex_encode($string)
    return bin2hex($string);

function hex_decode($string)
    return pack("H*", $string);
Categories: PHP Tags:

CodeIgniter: Your recaptcha.php file does not appear to contain a valid configuration array

October 28th, 2013 No comments

For those who have run into this problem as I do, check the settings for PHP marker (<? and <?=).
If not activated, you will see this error because they are placed in reCAPTCHA scripts.

Modify “<?” into “<?php” and “<?=” into “<?php echo” and everything will be fine.
The other option is to allow shorter markers.

Categories: PHP Tags: ,

Create PHP object without class

July 20th, 2013 No comments

Within JavaScript, you can easily create an object without the class as follows:

myObj = {}; = "Here we go";

Within PHP, you can do the following:

$myObj = new stdClass();
$myObj->abc = 'Here we go';

Date validation in PHP

May 10th, 2013 No comments

What happens if a user enters as birthday 31/02/2000.?
Is the date will be properly stored in the database?

Of course not. 03.03.2000 will be saved, because we have 3 days exceeded the number of days in February. It would be nice to warn him of the mistake. To do this, they would have to make a function that would control date.

PHP already has a function to check the validity date:

bool checkdate(int $month, int $day, int $year);

For the month, day and year, the function will return if the date is correct or not.

Categories: PHP Tags: ,

Get the controller and method name in CodeIgniter

May 10th, 2013 No comments

CI contains functions which see the link structure and extracting from it the name of the controller and the method that are called. These are the functions:

$this->Router->fetch_class(); // Class = controller
[/ code]

What is important, if you are using CI routing, these two functions will recognize this and send you the correct name of the controller and its methods. Example:

For link:
Routing redirects to:
CI will return as a controller “products” and as a method “view” .

For more data about routing, go to CI URI Routing.

Categories: PHP Tags: , ,

Filtering content from PHP variables

April 28th, 2013 No comments

PHP function filter_var() first appears in PHP 5.2.0 and is related to content filtering. For more information, see here. It is a function that will check if the content is inserted properly and take the appropriate action depending on the options you have defined.

filter_var mixed (mixed $variable[, int $filter=FILTER_DEFAULT[, mixed $options]]);
[/ code]

The function accepts three parameters. The first is the content that you want to control, second is the filter, and the third are the options that further describe the filter.

There are 4 types of filters:

  • Validate filters
  • Sanitize filters
  • Other filters
  • Filter flags

Validate filters

This is a filter that checks whether the entered text is really what you expect it to be. These are:

  • FILTER_VALIDATE_BOOLEAN – Returns TRUE for “1”, “true”, “on” and “yes”. Returns FALSE otherwise. (This is convenient for checking the value that is passed checkbox within the ExtJS framework)
  • FILTER_VALIDATE_FLOAT – Validates value as float
  • FILTER_VALIDATE_INT – Validates value as integer, optionally from the specified range
  • FILTER_VALIDATE_IP – Validates value as IP address, optionally only IPv4 or IPv6 or not from private or reserved ranges
  • FILTER_VALIDATE_URL – Validates value as URL (according to », optionally with required components. Beware a valid URL may not specify the HTTP protocol http:// so further validation may be required to determine the URL uses an expected protocol, e.g. ssh:// or mailto:. Note that the function will only find ASCII URLs to be valid; internationalized domain names (containing non-ASCII characters) will fail
  • FILTER_VALIDATE_EMAIL – Validates value as e-mail
  • FILTER_VALIDATE_REGEXP – Validates value against regexp, a Perl-compatible regular expression.

Some of these filters will be implemented in the new version of CodeIgniter Framework (v3) which will further affect his speed.

Sanitize filters

These are the filters that, in addition to checking the correctness, perform an additional action for the purpose of adapting to become correct. These are:

  • FILTER_SANITIZE_EMAIL – Remove all characters except letters, digits and !#$%&’*+-/=?^_`{|}~@.[]
  • FILTER_SANITIZE_ENCODED – URL-encode string, optionally strip or encode special characters
  • FILTER_SANITIZE_MAGIC_QUOTES – Apply addslashes()
  • FILTER_SANITIZE_NUMBER_FLOAT – Remove all characters except digits, +- and optionally .,eE
  • FILTER_SANITIZE_NUMBER_INT – Remove all characters except digits, plus and minus sign
  • FILTER_SANITIZE_SPECIAL_CHARS – HTML-escape ‘”<>& and characters with ASCII value less than 32, optionally strip or encode other special characters
  • FILTER_SANITIZE_FULL_SPECIAL_CHARS – Equivalent to calling htmlspecialchars() with ENT_QUOTES set. Encoding quotes can be disabled by setting FILTER_FLAG_NO_ENCODE_QUOTES. Like htmlspecialchars(), this filter is aware of the default_charset and if a sequence of bytes is detected that makes up an invalid character in the current character set then the entire string is rejected resulting in a 0-length string. When using this filter as a default filter, see the warning below about setting the default flags to 0
  • FILTER_SANITIZE_STRING – Strip tags, optionally strip or encode special characters
  • FILTER_SANITIZE_STRIPPED – Alias of “string” filter
  • FILTER_SANITIZE_URL – Remove all characters except letters, digits and $-_.+!*'(),{}|\\^~[]`<>#%”;/?:@&=
  • FILTER_UNSAFE_RAW – Do nothing, optionally strip or encode special characters. This filter is also aliased to FILTER_DEFAULT

Other filters

If you want to create your own function to filter, this is the section for you. As the filter is introduced FILTER_CALLBACK and as an extra option specifies a function that will execute the filtering/validation.

Filter flags

These are flags that can be used as an optional third parameter. A detailed list can be viewed here.

This is only an introduction to the possibilities that this feature provides. For more information, see the description of the functions in the framework of the official documentation.

Optimize HTML code

April 28th, 2013 No comments

Here’s a simple way to delete all redundant spaces, lines, tabs, and all the signs that are not visible when viewing the HTML page, but if you watch its source code are very much visible. The result is the HTML code into a single line, which can be reduced by a few KB:

preg_replace("/\s+/", " ", $string);

$string is a variable in which you store the HTML code.

Categories: PHP Tags: ,

(srpski) 10 saveta za optimizaciju PHP koda

March 10th, 2013 No comments

Sorry, this entry is only available in Serbian.

Categories: PHP Tags: ,

Moneybookers payments, part I: Create account

February 7th, 2012 No comments

Create two email accounts, one for the seller, one for the buyer.

Create an account on, the seller (business account) and for the customer using e-mail addresses which have been previously created.

Then you need to send a request to email of technical support: to change the status of the accounts. The mail should be noted that the accounts will be used to test purposes.

After sending the request I received the first email that my request pending and will get back to me.

Categories: PHP Tags:

(srpski) Sprečavanje keširanja CSS i JavaScript dokumenata

February 4th, 2012 No comments

Sorry, this entry is only available in Serbian.

Categories: PHP Tags: , ,