Pages

Calculate the similarity between two strings

Calculate the similarity between two strings and return the matching characters:

<?php
echo similar_text("Hello World","Hello Peter",$percent);
echo "<br /> Percent : ".$percent;
?>

Find and Correct Misspelled Words with Pspell in PHP

Every one of us has made a spelling mistake in a Google search: "alternitive music", for example. In doing so, you may have noticed that Google was trying to help you by displaying: "Did you mean alternative music?". If your site has a search function, to indicate misspellings if no or too few results have been found is a very useful feature, especially if the bad English of a visitor can make you miss a sale. Fortunately, PHP's Pspell module allows for checking the spelling of a word and suggesting a replacement from its default dictionary (you can also create a custom dictionary).
To begin, we need to check if Pspell is installed:
<?php
$config_dic= pspell_config_create ('en');
If you get an error, it isn't. On Linux systems, follow these instructions to solve the problem.
Use the default dictionary
Here is a small function to help you understand how Pspell works:
<?php
function orthograph($string)
{
    // Suggests possible words in case of misspelling
    $config_dic = pspell_config_create('en');

    // Ignore words under 3 characters
    pspell_config_ignore($config_dic, 3);

    // Configure the dictionary
    pspell_config_mode($config_dic, PSPELL_FAST);
    $dictionary = pspell_new_config($config_dic);

    // To find out if a replacement has been suggested
    $replacement_suggest = false;

    $string = explode('', trim(str_replace(',', ' ', $string)));
    foreach ($string as $key => $value) {
        if(!pspell_check($dictionary, $value)) {
            $suggestion = pspell_suggest($dictionary, $value);

            // Suggestions are case sensitive. Grab the first one.
            if(strtolower($suggestion [0]) != strtolower($value)) {
                $string [$key] = $suggestion [0];
                $replacement_suggest = true;
            }
        }
    }

    if ($replacement_suggest) {
        // We have a suggestion, so we return to the data.
        return implode('', $string);
    } else {
        return null;
    }
}
To use this function, it is sufficient to pass to it a string parameter:
<?php
$search = $_POST['input'];
$suggestion_spell = orthograph($search);
if ($suggestion_spell) {
    echo "Try with this spelling : $suggestion_spell";
}
If the string you submit to Pspell is "here is my mispellid word", the previous script will return: "Try with this spelling: Here is my misspelled word." However, Pspell is no miracle worker, especially if you're automatically using the first suggested spelling alternative! For best results, you can use all the suggestions offered by Pspell. The following script returns twenty proposals around the word "lappin":
<?php
$dict = pspell_new ("en");
if (!pspell_check ($dict, "lappin")) {
    $suggestions = pspell_suggest ($dict, "lappin");
     foreach ($suggestions as $suggestion) {
        echo "Did you mean: $suggestion?<br />";
     }
}
You must configure a dictionary to initialize Pspell. To do this, create a descriptor toward a configuration file of the dictionary, change some options of this descriptor, then use the configuration dictionary to create a second descriptor for the real dictionary. If this sounds a bit complicated, do not worry: The code rarely changes and you can usually copy it from another script. However, here we will study it step by step. Here is the code
that configures the dictionary:
    // Suggests possible words in case of misspelling
    $config_dic = pspell_config_create('en');

    // Ignore words under 3 characters
    pspell_config_ignore($config_dic, 3);

    // Configure the dictionary
    pspell_config_mode($config_dic, PSPELL_FAST);
$config_dic is the initial template which controls the options for your dictionary. You must load all the options in $config_dic, then use it to create the dictionary. pspell_config_create() creates an English dictionary (en). To use the English language and specify that you prefer American spelling, specify ‘en’ as the first parameter and 'american' as the second. pspell_config_ignore() indicates that your dictionary will ignore all words of 3 letters or less. Finally, pspell_config_mode() indicates to Pspell the operating mode:
• PSPELL_FAST is a quick method that will return the minimum of suggestions.
• PSPELL_NORMAL returns an average number of suggestions at normal speed.
• PSPELL_SLOW provides all possible suggestions, although this method takes some time to perform the spell check. We could still use other configuration options (to add, for example, a custom dictionary, as we shall see later), but as this is a quick check, we will simply create the dictionary with this line:
    $dictionary = pspell_new_config($config_dic);
From this point you can use the dictionary in two ways:
1. pspell_check($dictionary, "word") returns true if "word" is in the dictionary.
2. pspell_suggest($dictionary, "word") returns an array of suggested words if "word" is not in the dictionary (the first element of this array is the most likely candidate). The number of words obtained varies, but you get more with PSPELL_SLOW and fewer with PSPELL_FAST.
Now that the dictionary is ready, we cut the string that was passed as a parameter to obtain an array of words: ‘here my sentence‘ becomes an array of three elements, "here", "my", and "sentence". Then we check the spelling of each word using the default dictionary. Because it does not like commas, we also delete them before exploding the string. If the word has more than three characters, verification takes place and in case of misspelling, we conduct the following operations:
  1. We ask Pspell to provide an array of suggestions for correction.
  2. We take the most likely suggestion (the first element of the array $suggestion) and we replace the misspelled word with it.
  3. We set the $replacement_suggest flag to true so that at the end of the processing loop, we know that we have found a spelling mistake somewhere in $string. At the end of the loop, if there were spelling corrections, we are reforming the string from elements of the corrected array and we return this chain. Otherwise, the function returns null to indicate that it has not detected misspelling.
Add a custom dictionary to Pspell
If a word is not in the default dictionary, you can easily add it. However, you can also create a custom dictionary to be used with the default.
Create a directory on your site where PHP has the right to write and initialize the new dictionary in it. To create a new dictionary file called perso.pws in the directory path of your server, use the following script:
<?php
$config_dic = pspell_config_create ('en');
pspell_config_personal($config_dic, 'path / perso.pws');
pspell_config_ignore($config_dic , 2);
pspell_config_mode($config_dic, PSPELL_FAST);
$dic = pspell_new_config($config_dic);
This is the same script as in the previous section, but with an essential addition: calling pspell_config_personal() initializes a personal dictionary file. If this file does not already exist, Pspell creates a new one for you. You can add to this dictionary as many words as you want by using the following function:
`pspell_add_to_personal($dic, "word");`
As long as you have not saved the dictionary, words are added to it temporarily. Therefore, after inserting the words you want, add this line to the end of the script:
pspell_save_wordlist($dic);
Then call pspell_config_personal() as above in the demo script and your new dictionary will be ready.

Deploying Your First PHP Application with the Windows Azure Command Line Tools for PHP

Deploying PHP

Creating a Windows GUI Application with PHP



This is a guest blog post by Tim. Tim is a PHP programmer who is interested in Web Design, CSS, and Pizza. He runs the site php.uni.cc where he posts tutorials to help you with PHP, Design, and more. You can also find him over at forums.tizag.com.
Update: This was originally posted towards the end of July 2007. I'm reposting this as I believe it didn't get the attention it deserved. -Jake
This tutorial will show you how to make a basic “Hello World” application using PHP, Winbinder, and the Bambalam Compiler.
First we need to setup our environment. Once you have downloaded Winbinder and Bambalam Compiler from the links above, you should copy the following files into a new folder with the following structure:
  • project_folder/bam.exe #Bambamlam Compiler - rename the old .exe to bam.exe
  • project_folder/helloworld.php #Our “Hello World” source
  • project_folder/winbinder.php #Winbinder - from WinBinder-0.46.0/phpcode/include/
  • project_folder/wb_resources.inc.php #Winbinder - from WinBinder-0.46.0/phpcode/include/
  • project_folder/wb_generic.inc.php #Winbinder - from WinBinder-0.46.0/phpcode/include/
  • project_folder/wb_windows.inc.php #Winbinder - from WinBinder-0.46.0/phpcode/include/
  • project_folder/php_winbinder.dll #Winbinder - from WinBinder-0.46.0/binaries/php4/ext/
  • project_folder/project.bcp #Our Bam. project file
  • project_folder/build.bat #Command to build our project
Once you have the files in order, copy and paste the following lines into the corresponding files.
build.bat

bam project.bcp

pause
On running the file, it will build a .exe based on the parameters in project.bcp and will pause the output so we can see if there are any errors.
project.bcp

OUTFILE helloworld.exe

EMBED helloworld.php

EMBED wb_generic.inc.php

EMBED wb_resources.inc.php

EMBED wb_windows.inc.php

EMBED winbinder.php

MAINFILE helloworld.php

EXTENSION php_winbinder.dll
WINDOWED
This simply tells bam.exe to:
  • Output the .exe to helloworld.exe
  • Embed all the .php files
  • Set helloworld.php as the main file
  • Include the Winbinder extension
  • Not to show the command line
Now that our environment is setup, we can move onto the coding.
First, Open up helloworld.php with you favorite PHP editor.
As you would start off any PHP script, type in:
<?php
Now we need to include winbinder.php. This holds several functions that we need.
<?php

include(”winbinder.php”);
Next, we need to create the application window. We also need to set it in a function.
<?php
include(”winbinder.php”);

$window = wb_create_window(NULL, AppWindow, “Hello World”, WBC_CENTER, WBC_CENTER, 400, 300);
The function wb_create_window has several required parameters. I will explain them now.
  • NULL = The parent window. Since we have no other window, this needs to be NULL.
  • AppWindow = This is the window type. You can find more types in the WinBinder documentation.
  • “Hello World” = The window title.
  • WBC_CENTER = The X starting location. WBC_CENTER is a predefined constant
  • WBC_CENTER = The Y starting location. WBC_CENTER is a predefined constant
  • 400 = The window width
  • 300 = The window height
We will now make the text “Hello World” appear in the window.
<?php

include(”winbinder.php”);

$window = wb_create_window(NULL, AppWindow, “Hello World”, WBC_CENTER, WBC_CENTER, 400, 300);

$hello_world = wb_create_control($window, Label, “Hello World”, 5, 5, 100, 50);
The function wb_create_control has several required parameters. I will explain them now.
  • $window = The objective window. Put the variable of the window you wish to add the control to in that spot.
  • Label = The control type. You can find more control types in the WinBinder documentation.
  • “Hello World” = The default value
  • 5 = The X starting location.
  • 100 = The control width
  • 50 = The control height
Almost done! Finally, we must add the following function that loops the window to keep it alive.
<?php

include(”winbinder.php”);

$window = wb_create_window(NULL, AppWindow, “Hello World”, WBC_CENTER, WBC_CENTER, 400, 300);

$hello_world = wb_create_control($window, Label, “Hello World”, 5, 5, 100, 50);

wb_main_loop();
And there you have it! Now, compile the application by running build.bat. It all went well, it should output helloworld.exe which you should see the window with “Hello World”.
If you wish to expand you’re application making skills, you should read the Bambalam Compile project page and also the WinBinder documentation.


Creating JQuery form in Zend Framework


While few months back, I wrote an article on how to make Dojo Form in Zend Framework. Although dojo has numerous features, however most of the developers around prefer JQuery and prototype.
When Zend provide JQuery extension I wrote and article on how to use JQuery date picker.
While most of guys visited that article demand writing an article on how to create JQuery form in Zend Framework.
So I’m here to show you how to use JQuery extension provide with latest version of Zend Framework for creating wonderful JQuery form.
You will need to follow the steps bellow to create JQuery form.
1. Placing ZendX directory in right place
2. Make necessary configuration in bootstrap file
3. Write necessary code in your layout.phtml file.
4. Create JQuery form
5. and show that form in the template.

Placing ZendX directory in right place:
When you download latest version of Zend Framework and extract the zip file. You will see a directory called “extras”. When open that directory you will find ZendX folder. Copy this to your
Library/ folder at the same level of your Zend directory
You directory structure will be like this.
Library/
Zend/
ZendX/

Making necessary configuration in bootstrap:
After placing the directory you will need to add a bit of code in your bootstrap file.
Add the following lines to your bootstrap file.
$view = new Zend_View();
$view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

In the code above, we instantiate our Zend_View object, set helper path, add view to the viewRenderer and finally add viewRenderer the helper broker.
Keep in mind that if you have already instantiate Zend view in your bootstrap, you don’t need to instantiate it twice.
Write necessary code in your layout.phtml file:
The only line you will need to include in your layout file is
echo $this->jQuery();

If you don’t use two step view, you can include this line at the top of each of your view template file instead.
Create JQuery form:
Now you have done all the necessary configuration, its time to create the actual form.
Create JQuery form in Zend framework is piece of cake.
If you want to create form in your controller, write the following code.
$form = new ZendX_JQuery_Form();
$date1 = new ZendX_JQuery_Form_Element_DatePicker(
'date1',
array('label' => 'Date:')
);
$form->addElement($date1);
$elem = new ZendX_JQuery_Form_Element_Spinner(
"spinner1",
array('label' => 'Spinner:')
);
$elem->setJQueryParams(array(
'min' => 0,
'max' => 1000,
'start' => 100)
);
$form->addElement($elem);
$this->view->form = $form;

In the code above we have created our JQuery form object, and add two element date and spinner to it. And then assigned the form to the view template file. Although you can create the form in your controller, however I will strongly discourage it. I will prefer using separate php file and add following code to that file.
class JQueryForm extends ZendX_JQuery_Form
{
public function init()
{
$this->setMethod('post');
$this->setName('frm');
$this->setAction('path/to/action');

$date1 = new ZendX_JQuery_Form_Element_DatePicker(
'date1',
array('label' => 'Date:')
);

$this->addElement($date1);

$elem = new ZendX_JQuery_Form_Element_Spinner(
"spinner1",
array('label' => 'Spinner:')
);

$elem->setJQueryParams(array('min' => 0, 'max' => 1000, 'start' => 100));
$this->addElement($elem);
}
}

We have extended our form from ZendX_JQuery_Form, set its method, name and action and add two elements date and spinner.
Now in your controller/action
    $form = new JQueryForm();

$this->view->form = $form;

Showing form in the template:
In your view template file add only the following line.
    <?php

echo $this->form;

?>