Wednesday, October 05, 2005

PHP Tips -- String & Time Functions

String Functions

string trim($str);
string ltrim($str);
string rtrim($str);
int strlen($str)

string strtoupper($str);
string strtolower($str);
// make the first letter of $str uppercase
string ucfirst($str);
// make the first letter of every word in $str uppercase
string ucwords($str);

string nl2br($str);
string wordwrap($str[, $maxlinelen, $linebrkStr, $bBreakLongWord]);

false/string strstr($srcstr, $substr);
//case sensitive, return false if $substr not in $srcstr
// return the portion of the source string begining with the substring.
// Ex. strstr("abcd","bc") return "bcd"

false/string stristr($srcstr, $substr);
//not case sensitive, return false if $substr not in $srcstr

false/int strpos($srcstr, $substr, $index=0);

string substr($str, $index[, $len]);
string substr_replace($str, $rplace_str, $index, $len);
string str_replace($search_str(or a str array), $rplace_str(or a str array), $source_str(or a str array)); //replace all

$str_array = explode($delimiter, $str);

string/flase strtok([$str,] $delimiters)
Ex:

$test = "http://www.test.com/test.php?op=window&ver=5.0&db=sql&dev=php+mysql";
$delims = "?&";

$word = strtok($test, $delims);
//first time call, give the string as a para, the string will be catched.

// if $test contain empty string in it, should pass here
while(is_string($word)) {
if($word) { //not an empty string
echo "$word
";
}
// After the first time call, only give the delimiters
$word = strtok($delims);
}


string strip_tags()
$test = "<p>test: this <b>paragraph</b><br /> will return a line</p>";
echo "<pre>before: $test </pre>";
echo strip_tags($test, "<br />");


$billnum = "TAASD000012";
sscanf($billnum, "%1s%10s", $w1,$w2);
$counter = $w2 + 0;
// when $w2 isn't a number("AASD000012"), $counter will be 0


* ----------------------------------------------------- *
number_format -- Format a number with grouped thousands
Description
string number_format ( float number [, int decimals [, string dec_point, string thousands_sep]] )

number_format() returns a formatted version of number. This function accepts either one, two or four parameters (not three):

If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.

If two parameters are given, number will be formatted with 'decimals' decimals with a dot (".") in front, and a comma (",") between every group of thousands.

If all four parameters are given, number will be formatted with 'decimals' decimals, dec_point instead of a dot (".") before the decimals and thousands_sep instead of a comma (",") between every group of thousands.

Only the first character of thousands_sep is used. For example, if you use foo as thousands_sep on the number 1000, number_format() will return 1f000.
* ----------------------------------------------------- *

1 comment:

Chris D. said...

Time Functions

//current date and time
timestamp time();

tiemstamp mktime(hr, min, sec, mon, day, yr);

bool checkdate(mon, day, yr);
date_array getdate(timestamp=time());

string date(formatstr[, timestamp]);

//date in GMT
string gmdate(formatstr, timestamp);