Thursday, December 10, 2009

String Tokenizer in Java

I ran into the need to make a String tokenizer with java a few days ago. So here are a few snippets that can be used.

If you would like to include the delimiters use this function:
import java.util.regex.*;
public static String[] tokenize(String str, String pattern)
{
    return Pattern.compile("(?=("+pattern+"))|(?<=("+pattern+"))").split(str);
}
 Example:
tokenize("4+3+43/5-ab", "[\\+\\-/]+");
will return an array like this [4,+,3,+,43,/,5,-,ab]


If you would like to return the array without the delimiters included use this:
import java.util.regex.*;
public static String[] tokenizeSplit(String str, String pattern)
{
    return Pattern.compile(pattern).split(str);
}
 Example: 
tokenizeSplit("4+3+43/5-ab", "[\\+\\-/]+");
will return an array like this [4,3,43,5,ab]

Wednesday, December 9, 2009

Send Email With PHP

PHP mail function has two implementations:
bool mail ( string $to , string $subject , string $message )
bool mail ( string $to , string $subject , string $message , string $headers)
 
There is also room for additional parameters:

RETURN:
returns true is the email appears to be valid. (If you send something to address@blah.com, it appears to be valid so this function would return true even though the mail won't be delivered)

<?php
$to      
= "address@blah.com";
$subject = "Email sent from PHP";
$message = "Hi there!";
$headers = "From: someone@example.com";

mail($to$subject$message$headers);
?>

Sunday, October 25, 2009

Centering a DIV in Internet Explorer 5 through IE 8

As with many CSS Layouts, the need to center a div is common. The most widely used way to approach this problem is to use the margin attribute in CSS.

The following code is an approach to this problem.

<style>
.container
{
   width:500px;
   height:100px;
   background-color:green;
}
.box
{
   width:100px;
   height:100px;
   background-color:red;
   margin:auto 0px;
}
</style>
<div class="container">
   <div class="box">
   </div>
</div>

Notice the "margin: 0px auto" on the .box class. This tells the browser that you want the top and bottom margins of the .box class to be 0px and the left and right to automatically be set to center the box.

Well this wouldn't be an issue, but in IE 5, 6, 7, and 8 it looks like this:


In every other browser the result is as expected:


The fix for this is simple, just put a text-align attribute on the "container" and set its value to "center".

<style>
.container
{
   width:500px;
   height:100px;
   background-color:green;
   text-align:center;
}
.box
{
   width:100px;
   height:100px;
   background-color:red;
   margin:auto 0px;
}
</style>
<div class="container">
   <div class="box">
   </div>
</div>

And now it will look the same in all browsers:


NOTE: There are multiple ways to do this, but this has been the easiest one I've encountered.

Float left doesn't expand parent DIV

Float left doesn't expand parent DIV
A very common cross browser issue I've seen has been with floating DIVs. This is one of those inconsistencies between the popular browsers that a web developer has to keep in mind every time they develop a new CSS layout. It's very frustrating to create a layout and set a div to float left, but it doesn't expand it's parent DIV if you expect it to. Well it's not supposed to. If you develop in internet explorer you may expect that to work but if you try it in other browsers you will quickly be corrected.

For example. If you are developing your site in Internet Explorer or Opera, not sure why you would, you may do something like the following:

<style>
.container
{
   width:300px;
   background-color:green;
}
.box
{
   float:left;
   width:100px;
   height:100px;
   border:3px solid red;
}
</style>
<div class="container">
   <div class="box"></div>
   <div class="box"></div>
</div>

Now, if you were developing this in IE or Opera, you would think all is well.


But, you may be unaware of the results in Firefox, Safari, and Chrome.



Firefox, Safari, and Chrome all think that a floating div should not stretch the size of its parent div. While IE and Opera think that if a floating div is larger than its parent div, it should stretch. The whole goal to writing a layout like this is for it to work in all of the main browsers. In order to do that you need to write something that they all understand. For this problem, the solution is easy. To accommodate both ideas, all you have to do is use the CSS attribute called overflow and set it to hidden on the "container" div.

So the correct code would look something like this:

<style>
.container
{
   width:300px;
   background-color:green;
   overflow:hidden;
}
.box
{
   float:left;
   width:100px;
   height:100px;
   border:3px solid red;
}
</style>
<div class="container">
   <div class="box"></div>
   <div class="box"></div>
</div>
Now in all browsers your result is:

NOTE: the code I write in my articles are not HTML strict. It is just to get you started. If you want to use it, it is best practice to put the proper HTML tags to accompany my code.