php 操作xml文件的方法介绍。
1、xml文件
<班级>
<学生 number="101">
<名字>孙悟空</名字>
<名字>孙行者</名字>
<年龄>猴精猴精</年龄>
<介绍></介绍>
</学生>
<学生 number="102">
<名字>白骨精</名字>
<年龄>140</年龄>
<介绍>幻化万千</介绍>
</学生>
<学生 number="103">
<名字>猪八戒</名字>
<名字>猪无能</名字>
<年龄>200</年龄>
<介绍>能吃会睡</介绍>
</学生>
</班级>
2、修改xml文件的自定义类
<?php
/**
修改xml文件
@link http://www.
*/
class xmlDom{
public $version;
public $encoding;
private $xml;
private $items;
private $seachNode = '';
private $seachItem = '';
private $seachValue = '';
public $writeBytes = 0;
function __construct($xmlFile ='', $version ='1.0', $encoding = 'UTF-8'){
$this->version = $version;
$this->encoding = $encoding;
$this->xml = new DOMDocument($version, $encoding);
if($xmlFile)$this->xml->load($xmlFile);
}
function getRootEle($rootTag){
$this->xmlRoot = $this->xml->getElementsByTagName($rootTag)->item(0);
}
function getSeachItem($itemsTag, $seachNode, $seachValue){
$this->items = $this->xml->getElementsByTagName($itemsTag);
$this->items->length;
for($i=0; $i<$this->items->length; $i++){
$item = $this->items->item($i);//元素
$node = $item->getElementsByTagName($seachNode);//节点
for($j = 0; $j< $node->length; $j++){
$subNode = $node->item($j);
if($seachValue == $subNode->nodeValue){
$this->seachNode = $subNode;
$this->seachItem = $item;
$this->seachValue = $subNode->nodeValue;
break(2);
}
}
}
return ($this->seachNode) ? true : false;
}
function update($nodeValue, $nodeTag = '',$append = false, $index = 0){
if($append){
if($nodeTag)
$this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue += $nodeValue;
else
$this->seachNode->nodeValue += $nodeValue;
}else{
if($nodeTag)
$this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue = $nodeValue;
else
$this->seachNode->nodeValue = $nodeValue;
}
}
function save($filename){
$this->writeBytes = $this->xml->save($filename);
return ($this->writeBytes) ? true : false;
}
}
//调用示例
$test = new xmlDom('student.xml');
$test->getSeachItem('学生','年龄','103');//找到 年龄=103 的猪八戒
$test->update('小猪猪', '名字', false, 1); //把猪八戒的第二个名字改成:小猪猪
$test->save('new.xml'); //保存成新文件
?>
php取汉字首字母的代码。
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
/**
取汉字首字母
@link http://www.
*/
function getfirstchar($s0){
$fchar = ord($s0{0});
if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper()($s0{0});
$s1 = iconv("UTF-8","gb2312", $s0);
$s2 = iconv("gb2312","UTF-8", $s1);
if($s2 == $s0){$s = $s1;}else{$s = $s0;}
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if($asc >= -20319 and $asc <= -20284) return "A";
if($asc >= -20283 and $asc <= -19776) return "B";
if($asc >= -19775 and $asc <= -19219) return "C";
if($asc >= -19218 and $asc <= -18711) return "D";
if($asc >= -18710 and $asc <= -18527) return "E";
if($asc >= -18526 and $asc <= -18240) return "F";
if($asc >= -18239 and $asc <= -17923) return "G";
if($asc >= -17922 and $asc <= -17418) return "I";
if($asc >= -17417 and $asc <= -16475) return "J";
if($asc >= -16474 and $asc <= -16213) return "K";
if($asc >= -16212 and $asc <= -15641) return "L";
if($asc >= -15640 and $asc <= -15166) return "M";
if($asc >= -15165 and $asc <= -14923) return "N";
if($asc >= -14922 and $asc <= -14915) return "O";
if($asc >= -14914 and $asc <= -14631) return "P";
if($asc >= -14630 and $asc <= -14150) return "Q";
if($asc >= -14149 and $asc <= -14091) return "R";
if($asc >= -14090 and $asc <= -13319) return "S";
if($asc >= -13318 and $asc <= -12839) return "T";
if($asc >= -12838 and $asc <= -12557) return "W";
if($asc >= -12556 and $asc <= -11848) return "X";
if($asc >= -11847 and $asc <= -11056) return "Y";
if($asc >= -11055 and $asc <= -10247) return "Z";
return null;
}
function pinyin1($zh){
$ret = "";
$s1 = iconv("UTF-8","gb2312", $zh);
$s2 = iconv("gb2312","UTF-8", $s1);
if($s2 == $zh){$zh = $s1;}
for($i = 0; $i < strlen($zh); $i++){
$s1 = substr($zh,$i,1);
$p = ord($s1);
if($p > 160){
$s2 = substr($zh,$i++,2);
$ret .= getfirstchar($s2);
}else{
$ret .= $s1;
}
}
return $ret;
}
echo "欢迎您<br/>";
echo pinyin1('欢迎您');
?>
php汉字转拼音的实现代码
php批量获取首字母(汉字、数字、英文)的代码
将汉字转换成拼音的php函数
php操作mysql数据库的类。
<?
/**
@mysql数据库类
@link http://www.
*/
//header('content-type:text/html;charset=utf-8');
class mySql{
private $result;
private $conn;
public static $hasNew = false;
private __construct(){}
function __destruct(){
self::$hasnew=false;
}
function doNew(){
if(self::$have_new){
exit('数据库只能连接一次!');
}else{
self::$hasNew=true;
return new self;
}
}
private function connect($host,$user,$password,$dbname,$charset='utf8'){
$this->conn = mysql_connect()($host,$user,$password) or exit('错误码:'.mysql_errno(). '数据库连接失败:'.mysql_error());
mysql_select_db($dbname,$this->conn) or exit('错误码:'.mysql_errno().'选择数据库失败:'.mysql_error());
mysql_query()("set names $charset",$this->conn);
}
function query($sql,$buffer=true){
//mysql_real_escape_string($sql,$this->conn);//特殊字符义
if($buffer){
$this->result=mysql_query($sql,$this->conn) or exit('错误码:'.mysql_errno().'sql语句执行失败:'.mysql_error());
}else{
$this->result=mysql_unbuffered_query($sql,$this->conn) or exit('错误码:'.mysql_errno().'sql语句执行失败:'.mysql_error());
}
}
function getRecord(){
return mysql_fetch_array($this->result);
}
function close(){
mysql_free_result($this->result);
mysql_close($this->conn);
}
}
//数据库
$db_host='localhost';
$db_user='root';
$db_pwd='root';
$db_name='news';
$charset='utf8';
$sql="select * from news_base";
$db=mySql::doNew();
$db->connect($db_host,$db_user,$db_pwd,$db_name,$charset='utf8');
$db->query($sql);
while($row=$db->getRecord()){
echo $row[1].'<br />';
}
?>