Archive for the ‘PHP’ Category

PHP on Windows Training Kit (April 2009)

Wednesday, May 13th, 2009

Now Microsoft has published Training toolkit ie. PHP On Windows Training Kit (April 2009). This is a kit designed to help PHP developers can build applications in this language and platform for Windows and using IIS 7 and SQL Server 2008. The kit includes the following elements:

* PHP & SQL Server Demos.
* Integrating Geo-Spatial SQL Server with PHP.
* SQL Server Reporting Services and PHP.
* PHP & SQL Server Hands On Labs
* Introduction to Using SQL Server with PHP
* Full Text Search Using Office Documents in PHP over.
* PHP on Windows Hands On Labs
* IIS Access Control Features for PHP.
* Media Features Using IIS 7.0 in a PHP Application.
* Troubleshooting PHP.
* Migrating PHP Applications to IIS 7.0.

Link: Via Microsoft

Validate: Username, Email Id, Phone No, Zip Code & URL In 5 Languages(JS, C#.Net, VB.Net, JAVA & PHP)

Monday, March 23rd, 2009

Validate of Username, Email Id, Phone No, Zip/Postal Code & URL In Javascript, C#.Net, VB.Net, JAVA & PHP !

1. Validate username: Validate alpha numeric values
2. Validate email addresses : example@domain.com
3. Validate Phone numbers: Validate US phone number eg: 123-456-7890
4. Validate Zip/Postal Codes: Validate US Zip/Postal Code eg: 12345-6789
5. Validate Domain Name eg: http://live.com

Validate username: Validate alpha numeric values

?View Code JAVASCRIPT
function is_valid_username(str_username)
{
 var filter = /^([a-zA-Z\s0-9]*)$/;
 if (!filter.test(str_username))
 {
 alert(’Wrong username format.’);
 }
 else
 {
 alert('Username format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 
 private void is_valid_username(string str_username)
 {
 Regex matchRegex = new Regex(@"^[a-zA-Z0-9_]{3,16}$");
 MatchCollection matches  = matchRegex.Matches(str_username);
 if(matches.Count==0)
 {
 Response.Write("Wrong username format."); // In Webserver
 //MessageBox.Show("Wrong username format."); //In Windows Form
 }
 else
 {
 Response.Write("Username format is ok."); // In Webserver
 //MessageBox.Show("Username format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
 
Private Sub is_valid_username(ByVal str_username As String)
    Dim matchRegex As New Regex("^[a-zA-Z0-9_]{3,16}$")
    Dim matches As MatchCollection = matchRegex.Matches(str_username)
    If matches.Count = 0 Then
        Response.Write("Wrong username format.") ' In Webserver
        'MessageBox.Show("Wrong username format."); 'In Windows Form
    Else
        Response.Write("Username format is ok.")    ' In Webserver
        'MessageBox.Show("Username format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
 
public class is_valid_username
{
 public static void main(String str_username)
 {
 Pattern p=Pattern.compile("^[A-Za-z0-9]+$");
 Matcher m=p.matcher(str_username);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("Username format is ok.");
 }
 else
 {
 System.out.println("Wrong username format.");
 }
 }
}
Function is_valid_username($str_username)
{
 if (preg_match('/^[a-z\d_]{5,20}$/i',$str_username))
 {
     echo "Username format is ok.";
 }
 else
 {
     echo "Wrong username format.";
 }
}

Validate email addresses : example@domain.com

?View Code JAVASCRIPT
function is_valid_email(str_email_id)
{
 var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 if (!filter.test(str_email_id))
 {
 alert(’Wrong email address format.’);
 }
 else
 {
 alert('Email address format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 private void is_valid_email(string email_id)
 {
 Regex matchRegex = new Regex(@"[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}");
 MatchCollection matches  = matchRegex.Matches(email_id);
 if(matches.Count==0)
 {
 Response.Write("Wrong email address format."); // In Webserver
 //MessageBox.Show("Wrong email address format."); //In Windows Form
 }
 else
 {
 Response.Write("Email address format is ok."); // In Webserver
 //MessageBox.Show("Email address format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
 
Private Sub is_valid_email(ByVal email_id As String)
    Dim matchRegex As New Regex("[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}")
    Dim matches As MatchCollection = matchRegex.Matches(email_id)
    If matches.Count = 0 Then
        Response.Write("Wrong email address format.") ' In Webserver
        'MessageBox.Show("Wrong email address format."); 'In Windows Form
    Else
        Response.Write("Email address format is ok.")    ' In Webserver
        'MessageBox.Show("Email address format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_email
{
 public static void main(String str_email_id)
 {
 Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");
 Matcher m=p.matcher(str_email_id);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("Email address format is ok.");
 }
 else
 {
 System.out.println("Wrong email address format.");
 }
 }
}
Function is_valid_email($email_id)
{
 if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email_id))
 {
     echo "Email address format is ok.";
 }
 else
 {
     echo "Wrong email address format.";
 }
}

Validate Phone numbers: Validate US phone number eg: 123-456-7890

?View Code JAVASCRIPT
function is_valid_phone_no(str_phone_no)
{
 var filter =  /^((\+?1-)?\d\d\d-)?\d\d\d-\d\d\d\d$/;
 if (!filter.test(str_phone_no))
 {
 alert(’Wrong phone number format.’);
 }
 else
 {
 alert('Phone number format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 
 private void is_valid_phone_no(string str_phone_no)
 {
 Regex matchRegex = new Regex(@"\d{3}-\d{3}-\d{4}$");
 MatchCollection matches  = matchRegex.Matches(str_phone_no);
 if(matches.Count==0)
 {
 Response.Write("Wrong phone number format."); // In Webserver
 //MessageBox.Show("Wrong phone number format."); //In Windows Form
 }
 else
 {
 Response.Write("Phone number format is ok."); // In Webserver
 //MessageBox.Show("Phone number format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
Private Sub is_valid_phone_no(ByVal str_phone_no As String)
    Dim matchRegex As New Regex("\d{3}-\d{3}-\d{4}$")
    Dim matches As MatchCollection = matchRegex.Matches(str_phone_no)
    If matches.Count = 0 Then
        Response.Write("Wrong phone number format.") ' In Webserver
        'MessageBox.Show("Wrong phone number format."); 'In Windows Form
    Else
        Response.Write("Phone number format is ok.")    ' In Webserver
        'MessageBox.Show("Phone number format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_phone_no
{
 public static void main(String str_phone_no)
 {
 Pattern p=Pattern.compile("^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$");
 Matcher m=p.matcher(str_phone_no);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("Phone number format is ok.");
 }
 else
 {
 System.out.println("Wrong phone number format.");
 }
 }
}
Function is_valid_phone_no($str_phone_no)
{
 if (preg_match('/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x',$str_phone_no))
 {
     echo "Phone number format is ok.";
 }
 else
 {
     echo "Wrong phone number format.";
 }
}

Validate Zip/Postal Codes: Validate US Zip/Postal Code eg: 12345-6789

?View Code JAVASCRIPT
function is_valid_zip_code(str_zip_code)
{
 var filter =  /^\d\d\d\d\d-\d\d\d\d$/;
 if (!filter.test(str_zip_code))
 {
 alert(’Wrong Zip/Postal Code format.’);
 }
 else
 {
 alert('Zip/Postal Code format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 
 private void is_valid_zip_code(string str_zip_code)
 {
 Regex matchRegex = new Regex(@"\d{5}-\d{4}$");
 MatchCollection matches  = matchRegex.Matches(str_zip_code);
 if(matches.Count==0)
 {
 Response.Write("Wrong Zip/Postal Code format."); // In Webserver
 //MessageBox.Show("Wrong Zip/Postal Code format."); //In Windows Form
 }
 else
 {
 Response.Write("Zip/Postal Code format is ok."); // In Webserver
 //MessageBox.Show("Zip/Postal Code format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
Private Sub is_valid_zip_code(ByVal str_zip_code As String)
    Dim matchRegex As New Regex("\d{5}-\d{4}$")
    Dim matches As MatchCollection = matchRegex.Matches(str_zip_code)
    If matches.Count = 0 Then
        Response.Write("Wrong Zip/Postal Code format.") ' In Webserver
        'MessageBox.Show("Wrong Zip/Postal Code format."); 'In Windows Form
    Else
        Response.Write("Zip/Postal Code format is ok.")    ' In Webserver
        'MessageBox.Show("Zip/Postal Code format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_zip_code
{
 public static void main(String str_zip_code)
 {
 Pattern p=Pattern.compile("^(\\d{5})[- ]?(\\d{4})$");
 Matcher m=p.matcher(str_zip_code);
 boolean matchFound = m.matches();
if(matchFound)
 {
 System.out.println("Zip/Postal Code format is ok.");
 }
 else
 {
 System.out.println("Wrong Zip/Postal Code format.");
 }
 }
}
Function is_valid_zip_code($str_zip_code)
{
 if (preg_match('/^([0-9]{5})(-[0-9]{4})?$/i',$str_zip_code))
{
     echo "Zip/Postal Code format is ok.";
 }
 else
 {
     echo "Wrong Zip/Postal Code format.";
 }
}

Validate Domain Name eg: http://live.com

?View Code JAVASCRIPT
function is_valid_url(str_url)
{
 var filter =   /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/
 if (!filter.test(str_url))
 {
 alert(’Wrong URL format.’);
 }
 else
 {
 alert('URL format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 private void is_valid_url(string str_url)
 {
Regex matchRegex = new Regex(@"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
 MatchCollection matches  = matchRegex.Matches(str_url);
 if(matches.Count==0)
 {
 Response.Write("Wrong URL format."); // In Webserver
 //MessageBox.Show("Wrong URL format."); //In Windows Form
 }
 else
 {
 Response.Write("URL format is ok."); // In Webserver
 //MessageBox.Show("URL format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
 
Private Sub is_valid_url(ByVal str_url As String)
    Dim matchRegex As New Regex("(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?")
    Dim matches As MatchCollection = matchRegex.Matches(str_url)
    If matches.Count = 0 Then
        Response.Write("Wrong URL format.") ' In Webserver
        'MessageBox.Show("Wrong URL format."); 'In Windows Form
    Else
        Response.Write("URL format is ok.")    ' In Webserver
        'MessageBox.Show("URL format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_url
{
 public static void main(String str_url)
 {
 Pattern p=Pattern.compile("(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
 Matcher m=p.matcher(str_url);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("URL format is ok.");
 }
 else
 {
 System.out.println("Wrong URL format.");
 }
 }
}
Function is_valid_url($str_url)
{
 if (preg_match('/^([0-9]{5})(-[0-9]{4})?$/i',$str_url))
 {
     echo "URL format is ok.";
 }
 else
 {
     echo "Wrong URL format.";
 }
}

Create PHP Desktop Applications – Calculator

Thursday, March 5th, 2009

Introduction

As for PHP, Web is already known as a server-side language for creating applications, PHP-GTK2 and make use of the extension, PHP allows you to create applications that run on the desktop. In this paper, the PHP-GTK2 try to create a simple calculator for desktop use.

Audience

If you want to create PHP applications on the desktop

Environmental Building

Target OS is, Linux and Windows.

PHP-GTK2 is, PHP-GTK can be downloaded for free from our site.

For Windows, “php-gtk-2.**.**-win32-nts.zip” to download and unzip the file.

Basic configuration is at least close.

Sample program “Simple Calculator”

Then use the PHP-GTK2, let’s create a simple calculator. The following shows the source.

File name: calculator.php

<? php
class Calc (
private $ txt_1;
private $ txt_2;
private $ txt_3;
private $ tblTable;
private $ hbox_1;
private $ wnd1;
private $ cboCBox;
private $ calc_btn;
 
public function __construct () (
/ / Initialize the text box for numeric input
$ this-> txt_1 = new GtkEntry ();
$ this-> txt_2 = new GtkEntry ();
/ / Initialize display box solution for
$ this-> txt_3 = new GtkEntry ();
/ / Adjust the width of each text box,
$ this-> txt_1-> set_width_chars (6);
$ this-> txt_2-> set_width_chars (6);
$ this-> txt_3-> set_width_chars (6);
 
/ / Initialize the cboCBox box selection operator
$ this-> cboCBox = GtkcboCBoxBox:: new_text ();
$ this-> cboCBox-> insert_text (0 ,"+");
$ this-> cboCBox-> insert_text (1 ,"-");
$ this-> cboCBox-> insert_text (2 ,"×");
$ this-> cboCBox-> insert_text (3 ,"÷");
Appear to //"+" initial
$ this-> cboCBox-> set_active (0);
 
/ / Initialize the calculation button
$ this-> calc_btn = new GtkButton ("=");
 
/ / Assign the event listener
$ this-> calc_btn-> connect_simple ( "clicked", array ($ this, "calcHandler"));
 
/ / Initialize the table
$ this-> tblTable = new GtkTable (1, 5);
/ / Place the components on the table
$ this-> tblTable-> attach ($ this-> txt_1, 0,1,0,1);
$ this-> tblTable-> attach ($ this-> cboCBox, 1,2,0,1);
$ this-> tblTable-> attach ($ this-> txt_2, 2,3,0,1);
$ this-> tblTable-> attach ($ this-> calc_btn, 3,4,0,1);
$ this-> tblTable-> attach ($ this-> txt_3, 4,5,0,1);
 
$ this-> hbox_1 = new Gtkhbox_1 ();
$ this-> hbox_1-> pack_start ($ this-> tblTable);
$ this-> wnd1 = new GtkWindow ();
$ this-> wnd1-> add ($ this-> hbox_1);
$ this-> wnd1-> set_title ( "PHP Calculator");
$ this-> wnd1-> show_all ();
Gtk:: main ();
)
public function calcHandler () (
$ num1 = doubleval ($ this-> txt_1-> get_text ());
$ num2 = doubleval ($ this-> txt_2-> get_text ());
switch ($ this-> cboCBox-> get_active ()) (
/ / Add
case 0:
$ this-> txt_3-> set_text ($ num1 + $ num2);
break;
/ / Subtraction
case 1:
$ this-> txt_3-> set_text ($ num1 - $ num2);
break;
/ / Multiplication
case 2:
$ this-> txt_3-> set_text ($ num1 * $ num2);
break;
/ / Division default:
$ this-> txt_3-> set_text ($ num1 / $ num2);
)
)
)
/ / Calc, create an instance of the class
 
$ calc = new Calc ();
 
?>

Run the application

Let’s run this application. Open a command prompt, when you could just download the folder php-gtk2. Then, please run the following command:

C:\php-gtk2> php.exe calculator.php

Result

PHP App

If you want to quit the application, or applications [×] button on the command line [Ctr] + [C] Please run the shortcut key.

Commentary

Calc components are defined in the class.

Components used in this
GtkEntry Text box.
GtkComboBox Combo box.
GtkButton Button.
GtkTable Table. Placing a child in a grid component.
GtkHBox Horizontal box to hold the table.
GtkWindow Window to store the horizontal box.

GtkEntry and GtkComboBox, GtkButton and small components, GtkTable and place it in a container. To create a GtkTable this, new GtkTable(1,5) and a vertical length, the length of the next five and a specified grid. GtkTable to the placement of components, as follows: attach use the function.

$ this-> tblTable-> attach ($ this-> txt_1, 0,1,0,1);

Then put the components placed GtkHBox GtkTable objects. GtkHBox is the horizontal component of the box. The horizontal box is placed in the following pack_start use the function.

$ this-> hbox_1-> pack_start ($ this-> tblTable);

In addition, the box GtkHBox add this add to place a GtkWindow object.

$ this-> wnd1-> add ($ this-> hbox_1);

To set the title of the GtkWindow, set_title function.

$ this-> wnd1-> set_title ( "PHP Calculator");

Also, [=] to make the calculation for the function is called when pressing the button below.

$ this-> calc_btn-> connect_simple ( “clicked”, array ($ this, “calcHandler”));
Signal “clicked”, ie when I click “calcHandler” means that the function is called.

calcHandler Let’s look at the contents of the function.

$ num1 = doubleval ($ this-> txt_1-> get_text ());
$ num2 = doubleval ($ this-> txt_2-> get_text ());

txt_1, txt_2 the figures obtained in doubleval to quantify explicitly the function, each variable $ $num1 $ $num2 assigns.

Then, the index of the operator is selected in the combo box of the operator get_active Get function.

$ this-> cboCBox-> get_active ()

And in this case, the index

0 addition, if
If a subtraction
If you multiply two
If the three division
Has been set.

Calc Thus we complete the application to generate the last instance of the class.

$ calc = new Calc ();

Conclusion

This time I made a PHP-GTK2 applications running on their desktop calculator use. PHP-GTK in the rich component than what’s used.

PHP is a desktop application that is not made aware of the opportunity that this PHP-GTK2 to even try to challenge?

It may spread to new worlds.

References

PHP-GTK user guide