[旧文存档]多维度的IF语句重构

Categories: Development NotesJava; Tagged with: ; @ August 29th, 2010 18:05

多维度的IF语句重构 – 注释掉的为重构前的代码, 杂乱且可读性极低.

(more…)

PHP: Reading XML with DOM 使用DOM解析XML

Categories: PHP; Tagged with: ; @ August 28th, 2010 17:50

PHP中有很多方法解析XML. 本文单表使用DOM解析XML.

(more…)

PHP: Sending Html/plain Email by mb_send_mail 发送HTML/Plain格式的邮件

Categories: PHP; Tagged with: ; @ August 28th, 2010 14:55

使用mb_send_mail方法发送html/plain格式的Email, HTML格式的邮件.

本方法将根据trim后的邮件内容设置email类型: 如果以"<"开始, 则将邮件设置为Html格式, 反之为plain格式.

(more…)

PHP: 使用fopen进行文件读写

Categories: PHP; Tagged with: ; @ August 28th, 2010 11:48

1. 按行读取数据:

		// 读取上次报错时间
		try {
			$fhRead =  @fopen("timestamp/$this->id.txt", "r");
			if($fhRead) {
				// echo "时间记录文件第一行".fgets($fhRead)."
"; // echo "时间记录文件第二行".fgets($fhRead)."
"; $lastSendEmail = strtotime(fgets($fhRead)); $lastSendSms = strtotime(fgets($fhRead)); }else { $this->logMesg("文件有误, 无法读取"); } }catch (Exception $e) { $this->logMesg("读取报错时间记录文件遇到错误:".$e); }

2. 写入数据:

		// 记录报错时间.
		try {
			$fhMarkTime = @fopen("timestamp/$this->id.txt", "w");
			if($fhMarkTime) {
				fwrite($fhMarkTime, date("Y-m-d H:i:s",$upadtedLastSendEmail)."\n");
				fwrite($fhMarkTime, date("Y-m-d H:i:s",$updatedLastSendSms));
			}else {
				echo "文件写入错误.";
			}
		}catch (Exception $e) {
			echo "文件写入错误: ".$e;
		}

PHP Array 简单操作小结

Categories: PHP; Tagged with: ; @ August 28th, 2010 11:32

1. 创建Array: $arraySites = array();

2. addItem: $arraySites[$site->id] = $site;

3. 使用foreach遍历Array:

foreach ($arraySites as $site) { // 遍历每一个Site.
	// do something.
}

// Or:
foreach($array as $key=>$value)
{
echo "$key,$value ";
}

4. length: count($arraySite);

5.  打散/切割 Array : array_chunk($mails, $MAX_MAILS_PER_SMTP_SESSION, true);

该方法将会根据设定的大小对原数组进行切割, 生成多个小的数组(最后一个可能会少于单元大小), 第三个参数控制是否在打散后的数组内保留其原有key.

复制/移除 数组: http://liguoliang.com/2010/copy-and-remove-array-to-array-in-php/

Newer Posts <-> Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.