Monday, July 23, 2007

File Handling - Strip File Extension

This php code will strip the extension from a filename.
to remove the extension just call the function with the name of the file.
function strip_ext($name)
{
$ext = strrchr($name, '.');
if($ext !== false)
{
$name = substr($name, 0, -strlen($ext));
}
return $name;
}
// demonstration
$filename = 'file_name.txt';
echo strip_ext($filename)."\n";
// to get the file extension, do
echo end(explode('.',$filename))."\n";
?>

File Handling - Write to File

function to write a string to a file
function file_write($filename, $filecontent)
{
if($fp = @fopen($filename,"w"))
{
$contents = fwrite($fp, $filecontent, 80000);
fclose($fp);
return true;
}else{
return false;
}
}
?>

File Handling - Write to File First Line

This snippet let’s you insert data at the beginning of a file.
it reads the contents and adds new data to the first line of the file.
after this it joins the lines and writes them back to the file again.
// your new data + newline
$new_line = 'some new data here'."\n";
// the filepath
$file = 'emp/file.txt'
// the old data as array
$old_lines = file($file);
// add new line to beginning of array
array_unshift($old_lines,$new_line);
// make string out of array
$new_content = join(",",old_lines);
$fp = fopen($file,‘w’);
// write string to file
$write = fwrite($fp, $new_content);
fclose($fp);
?>

Friday, July 20, 2007

Spellchecking and text matching

As a web-based system, PHP primarily works with text in various languages: messageboards, IRC chats, web mail, etc. All of these lend themselves excellently to the task of spellchecking, so it should be no surprise that PHP has an extension specifically for it. The extension is based on the Aspell library (don’t ask; the names of spelling libraries has caused much confusion in the past!), so you need to download and install that before you start.

Once you have PHP configured with Pspell support, you can get started playing around with it. Although there are quite a few functions for the extension (see the PHP manual for more information), you really only need three: pspell_new(), pspell_check(), and pspell_replace().

int pspell_new ( string language [, string spelling [, string jargon [, string encoding [, int mode]]]]) bool pspell_check ( int dictionary_link, string word) array pspell_suggest ( int dictionary_link, string word)

Here’s a basic example of pspell_new() and pspell_check() in action. It creates a new English dictionary and checks if the word “Baboon” exists:

$pspell = pspell_new("en");

if (pspell_check($pspell, "Baboon")) {
echo "You can spell!";
} else {
echo "Back to school for you...";
}

?>

Note that you need to store the return value from pspell_new() in order to use it in later functions. The pspell_check() function takes the opened Pspell dictionary as its first parameter and the word to check as its second parameter. It then returns true if the word was found or false otherwise.

If we work in pspell_suggest() we can also have Pspell recommend alternatives to wrongly spelt words. You use pspell_suggest() in the same way as pspell_check(), except that it returns an array of suggestions rather than a boolean. Let’s extend that previous script a little:

$pspell = pspell_new("en");
$word = "babooon";

if (pspell_check($pspell, $word)) {
echo "You can spell!";
} else {
$suggestions = pspell_suggest($pspell, $word);
if (count($suggestions)) {
echo "I didn't understand '$word'. You probably meant one of these:n";

foreach($suggestions as $suggestion) {
echo " $suggestionn";
}
} else {
echo "Back to school for you...";
}
}
?>

Note that this time I’ve used “babooon” as the test word, which shouldn’t exist in the dictionary. This time, when pspell_check() fails, we run the word through pspell_suggest() and, if there are suggestions, print them out. Easy, huh?

Of course, the problem here is that you can only check one word at a time, which is pretty useless. What we really want to do is have our script take a string of text and break it up into individual words, checking each of them. Fortunately it’s not much harder than the previous script!

$pspell = pspell_new("en");
$sentence = "The quik brown fox jumpd over the lazyyy dog";
$words = explode(" ", $sentence);

foreach($words as $word) {
if (pspell_check($pspell, $word)) {
// this word is fine; print as-is
echo $word, " ";
} else {
// this word is bad; look for suggestions
$suggestions = pspell_suggest($pspell, $word);

if (count($suggestions)) {
// we have suggestions for this word; print them out
echo " ";
} else {
// no suggestions; just print the word
echo $word;
}
}
}
?>

Browser Detection

object get_browser ( [string user_agent])

You should already know that you can use the $_SERVER[’HTTP_USER_AGENT’] variable to see what web browser requested a page, but how can you tell whether that browser supports the HTML IFRAME element? Does that browser support CSS 2? What version number is it?

While you could glean a little more information by actually parsing the user agent, a much better solution is to let PHP do it for you! This is done using the get_browser() function, which, when given no parameters, parses the user agent from $_SERVER[’HTTP_USER_AGENT’]. Alternatively, you can pass it a particular user agent to work with. Either way, it returns an object that contains information about that particular web browser: what it’s capable of, what version it is, and what platform it’s running on.

The function works by looking up the user agent in its long list of browsers, and reading from there what that browser supports. You need to download this browser list yourself - the latest URLs are always kept in the PHP manual, but last time I checked it was http://www.garykeith.com/browsers/downloads.asp.

. Look in the manual for the get_browser() function, then search the page for “browscap.ini” - the name of the browser capabilities file. Download the file (at the time of writing, the link on the PHP site takes you to another site that has a PHP-specific browscap.ini file) and place it either in c:\Windows for Windows users or /etc for Unix users, then open up your php.ini file. In there, search for the line “[browscap]” and edit the next line to point to your new browscap.ini file. So, it might look like this:

browscap = c:/windows/browscap.ini

Save the file and restart your web server to have the new browscap file loaded. Now, onto the code! The easiest way to get started is just to dump out all the information so you get an idea what’s on offer:

>?php
echo "";
print_r(get_browser());
echo "";
?>

That will print out the long list of information made available to you. Here are some of the things printed out for me:

[parent] => IE 6.0
[platform] => WinXP
[netclr] => 1
[browser] => IE
[version] => 6
[majorver] => 6
[minorver] => 0
[css] => 2
[frames] => 1
[iframes] => 1

From that you can see I’m running IE6 on Windows XP with .NET installed, it supports CSS v2.0, as well as HTML frames and IFRAME elements. There are quite a few other things there, too - try it yourself for more.

Now, just dumping out information like that is no use to anyone - what you really want to do is get down to the nitty-gritty of actually using that information in a smart way. For example, we could use it to load a custom CSS file for the various browsers out there. Keep in mind that each browser usually has its own way of rendering CSS styles, so having different stylesheets for each browser makes a lot of sense!

Here’s some code to do just this:

$browser = get_browser();

switch ($browser->browser) {
case "IE":
switch ($browser->majorver) {
case 6:
case 5:
echo '';
break;
default:
echo '';
}

break;

case "Firefox":
case "Mozilla":
echo '';
break;

case "Netscape":
if ($browser->majorver < 5) {
echo '';
} else {
echo '';
}
break;

case "Safari":
case "Konqueror":
echo '';
break;

case "Opera":
echo '';
break;

default:
echo '';
}
?>

Monday, July 9, 2007

object get_browser ( [string user_agent])
You should already know that you can use the $_SERVER[’HTTP_USER_AGENT’] variable to see what web browser requested a page, but how can you tell whether that browser supports the HTML IFRAME element? Does that browser support CSS 2? What version number is it?
While you could glean a little more information by actually parsing the user agent, a much better solution is to let PHP do it for you! This is done using the get_browser() function, which, when given no parameters, parses the user agent from $_SERVER[’HTTP_USER_AGENT’]. Alternatively, you can pass it a particular user agent to work with. Either way, it returns an object that contains information about that particular web browser: what it’s capable of, what version it is, and what platform it’s running on.
The function works by looking up the user agent in its long list of browsers, and reading from there what that browser supports. You need to download this browser list yourself - the latest URLs are always kept in the PHP manual, but last time I checked it was http://www.garykeith.com/browsers/downloads.asp.
. Look in the manual for the get_browser() function, then search the page for “browscap.ini” - the name of the browser capabilities file. Download the file (at the time of writing, the link on the PHP site takes you to another site that has a PHP-specific browscap.ini file) and place it either in c:\Windows for Windows users or /etc for Unix users, then open up your php.ini file. In there, search for the line “[browscap]” and edit the next line to point to your new browscap.ini file. So, it might look like this:browscap = c:/windows/browscap.ini
Save the file and restart your web server to have the new browscap file loaded. Now, onto the code! The easiest way to get started is just to dump out all the information so you get an idea what’s on offer:>?php
echo "";
print_r(get_browser());
echo "";
?>
That will print out the long list of information made available to you. Here are some of the things printed out for me:[parent] => IE 6.0
[platform] => WinXP
[netclr] => 1
[browser] => IE
[version] => 6
[majorver] => 6
[minorver] => 0
[css] => 2
[frames] => 1
[iframes] => 1
From that you can see I’m running IE6 on Windows XP with .NET installed, it supports CSS v2.0, as well as HTML frames and IFRAME elements. There are quite a few other things there, too - try it yourself for more.
Now, just dumping out information like that is no use to anyone - what you really want to do is get down to the nitty-gritty of actually using that information in a smart way. For example, we could use it to load a custom CSS file for the various browsers out there. Keep in mind that each browser usually has its own way of rendering CSS styles, so having different stylesheets for each browser makes a lot of sense!
Here’s some code to do just this:$browser = get_browser();
switch ($browser->browser) {
case "IE":
switch ($browser->majorver) {
case 6:
case 5:
echo '';
break;
default:
echo '';
}
break;
case "Firefox":
case "Mozilla":
echo '';
break;
case "Netscape":
if ($browser->majorver < 5) {
echo '';
} else {
echo '';
}
break;
case "Safari":
case "Konqueror":
echo '';
break;
case "Opera":
echo '';
break;
default:
echo '';
}
?>
If I have to explain any of how that works, the PHP programming world is in serious trouble!