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);
?>