‘ PHP ’ category archive

How to write a custom PHP page in WordPress?

June 21, 08 by the programmer

WordPress is one of the most used blogging platform in the world.

It is very extensible throw the plugins and it is very easy to use.

However sometimes we need to make some custom pages that contain forms and other custom code that is more complicated to implement using plugins, it is much easier to implement them by simply creating a custom standalone wordpress page that will have all the wordpress standard headers footers, sitebars and everything else except that it will have a unique content.

This is very simple, all you have to do is create a PHP page in the WordPress root folder and copy the code bellow. That’s it .

The code bellow is very simple example of a page that contains a simple form with one text field and a submit button. When you click on the submit button the entered text will be displayed.

<?php
require_once ‘./wp-blog-header.php’;
get_header();
# CUSTOM CONTENT GOES HERE

$submited_value = $_REQUEST[’edb_text’];

$html_code = ‘
<div id=”content”>
‘.$submited_value.’
<form method=”post”>
<input type=”text” name=”edb_text” />
<input type=”submit” value=”Submit”>
</form>
</div>
‘;
print $html_code;

get_sidebar();
get_footer();
?>

Try it and have fun :)

Conditional subpatterns with PCRE in PHP

June 12, 08 by the programmer

PCRE (Perl Compatible Regular Expressions) library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5.

This library is used in many open source projects such as Apache Web Server and PHP.

Conditional subpatterns is something like an “if” statemant in regular expressions, which means that you can implement some conditions in your regular expression patterns and solve more complex problems in a simpler way.

According to http://www.php.net/manual/en/regexp.reference.php this is how you can implement a conditional statemant in a regular expression pattern

(?(condition)yes-pattern)
(?(condition)yes-pattern|no-pattern)

The question mark can be interpreted as “if”, so if the condition is satisfied then the yes-pattern will be used, if the condition is not satisfied then the no-pattern will be used if it exists

You can use the ?! for negative assertions.

This can be used to solve a lot of problems .

This is just an example that came to my mind. Find urls that are not http://minanov.com in some text.

$string = “

http://minanov.com

http://some_domain.com

some text

“;

preg_match(”/(?(?!http\:\/\/minanov\.com)http:\/\/.*| )/i”, $string, $matches);

print(”<pre>”.print_r($matches).”</pre>”);

The above print will return all the urls except the urls in the format http://minanov.com

Array ( [0] => http://some_domain.com )

PCRE Functions defined in PHP are

  • preg_grep — Return array entries that match the pattern
  • preg_last_error — Returns the error code of the last PCRE regex execution
  • preg_match_all — Perform a global regular expression match
  • preg_match — Perform a regular expression match
  • preg_quote — Quote regular expression characters
  • preg_replace_callback — Perform a regular expression search and replace using a callback
  • preg_replace — Perform a regular expression search and replace
  • preg_split — Split string by a regular expression

More details about those functions can be found here http://www.php.net/pcre

Remove duplicate entries from an Array in PHP

June 04, 08 by the programmer

If you want to remove duplicate items from an array in PHP

you can do it with the command array_unique

$array_with_duplicate_entries = array(1, 2, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10);

$array_with_no_duplicate_entries = array_unique($array_with_duplicate_entries);

The content of the array with removed entries will be

$array_with_no_duplicate_entries = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

Note that the indexes are preserved during the removal of the duplicate entries


Generate a unique id in PHP using uniqid()

May 25, 08 by the programmer

This is how you can generate a unique id in php using the uniqid() php function.

string uniqid ([ string $prefix [, bool $more_entropy ]] )

This function takes two parameters

- prefix (this string will be appended in front of each generated unique number ).

  • This parameter was made optional in PHP v5.0
  • The limit of 114 characters long for prefix was raised in PHP v4.3.1

- boolean more_entropy - If you set this to true the generated number will be longer (23 characters) if it is false it will be 13 characters

//This syntax without prefix can be used after PHP v5.0

$unique_id = uniqid();

// better, difficult to guess if you use the random number generator function for the prefix
$better_unique_id = uniqid(rand(), true);

Easy PHP MYSQL database comparation tool

May 20, 08 by the programmer

Every programmer comes to a situation when he has to compare two databases.

This can be the development and the production database in order to synchronize them or it can be any other two databases that needs to be structurally compared.

I found a very nice and simple free tool written in PHP that does the job very well.

The tool is called mysqldiff and it is a php application. Once you install it on your server you can compare any two MySQL databases.

Check it out.

You can download the tool from here

http://www.mysqldiff.org/downloads.php

Converting a normal IP Address to IP Long/decimal Format?

May 17, 08 by the programmer

IP addresses are expressed in dotted-decimal format - basically 4 sets of numbers from 0 to 255 seperated by periods

Another way of expressing an IP address is in a Long or Decimal 10 digit number format.

The decimal representation of the IP address is used because it is very easy to compare ranges of ip adresses that way.

So instead of comparing 192.168.1.1 to 192.168.1.255

it is much easier to compare 3232235777 to 3232236031

How is the decimal IP number generated from an IP address in a dotted format?

We will convert the IP address 192.168.0.1 from a dotted format to a decimal 10 digit format.

A.B.C.D = D + (C * 256) + (B * 256 * 256) + (A * 256 * 256 * 256) =

192.168.0.1 = 1 + (0 * 256) + (168 * 256 * 256) + (192 * 256 * 256 * 256) = 3232235521

This is a PHP function that will do that for you.

function ip_address_to_number($IPaddress) {

if ($IPaddress == “”) {

return 0;

} else {

$ips = split (\., “$IPaddress”);

return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);

}

}

Wait wait…

There is a much easier way of doing that in PHP. There is a already built in function in php just for that it’s called ip2long(). Check it out.

This applies only for IP v4 IP addresses

Have a nice conversion :)

error reporting in php

May 12, 08 by the programmer

Have you ever wanted to turn of the annoying warnings or notices that some php functions print.

There is a very simple command just for that.

int error_reporting ([ int $level ] )

This function takes as a parameter the error reporting level and returns the old error reporting level.

1           E_ERROR
2           E_WARNING
4           E_PARSE
8           E_NOTICE
16         E_CORE_ERROR
32         E_CORE_WARNING
64         E_COMPILE_ERROR
128       E_COMPILE_WARNING
256       E_USER_ERROR
512       E_USER_WARNING
1024     E_USER_NOTICE
6143     E_ALL
2048     E_STRICT
4096     E_RECOVERABLE_ERROR

This is how you can use it. Just write the command at the beggining of the php file and that’s it.

Some of the most used examples are listed bellow:

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings …)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set(‘error_reporting’, E_ALL);