当前位置:  编程技术>php
本页文章导读:
    ▪用PHP创建PDF中文文档       我使用的是FPDF(www.fpdf.org),下载了fpdf类库后,还要使用下面的中文类库才能支持中文,但只能使用一种中文字体(华文仿宋)。为此我烦恼了很长时间,现在终于搞定了,将TrueType字体转.........
    ▪PHP与javascript对多项选择的处理         我们经常要给用户作出多项选择进行处理,例如允许用户对列表项选择多项后删除选定项等。今天举个例子说明PHP和JavaScript分别是怎样处理多项选择的。今天我们做的是一个投票系统,.........
    ▪3       {                    if (is_array($value) && ! $delete)                    {                        foreach ($value as $suboption => $subvalue)   .........

[1]用PHP创建PDF中文文档
    来源: 互联网  发布时间: 2013-11-30

我使用的是FPDF(www.fpdf.org),下载了fpdf类库后,还要使用下面的中文类库才能支持中文,但只能使用一种中文字体(华文仿宋)。为此我烦恼了很长时间,现在终于搞定了,将TrueType字体转化为pt1字体使用:

下面是在FPDF上找的一个中文类库:
<?php
require('fpdf.php');

$Big5_widths=array(' '=>250,'!'=>250,'"'=>408,'#'=>668,'$'=>490,'%'=>875,'&'=>698,'''=>250,
'('=>240,')'=>240,'*'=>417,'+'=>667,','=>250,'-'=>313,'.'=>250,'/'=>520,'0'=>500,'1'=>500,
'2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>250,';'=>250,
'<'=>667,'='=>667,'>'=>667,'?'=>396,'@'=>921,'A'=>677,'B'=>615,'C'=>719,'D'=>760,'E'=>625,
'F'=>552,'G'=>771,'H'=>802,'I'=>354,'J'=>354,'K'=>781,'L'=>604,'M'=>927,'N'=>750,'O'=>823,
'P'=>563,'Q'=>823,'R'=>729,'S'=>542,'T'=>698,'U'=>771,'V'=>729,'W'=>948,'X'=>771,'Y'=>677,
'Z'=>635,'['=>344,''=>520,']'=>344,'^'=>469,'_'=>500,'`'=>250,'a'=>469,'b'=>521,'c'=>427,
'd'=>521,'e'=>438,'f'=>271,'g'=>469,'h'=>531,'i'=>250,'j'=>250,'k'=>458,'l'=>240,'m'=>802,
'n'=>531,'o'=>500,'p'=>521,'q'=>521,'r'=>365,'s'=>333,'t'=>292,'u'=>521,'v'=>458,'w'=>677,
'x'=>479,'y'=>458,'z'=>427,'{'=>480,'|'=>496,'}'=>480,'~'=>667);

$GB_widths=array(' '=>207,'!'=>270,'"'=>342,'#'=>467,'$'=>462,'%'=>797,'&'=>710,'''=>239,
'('=>374,')'=>374,'*'=>423,'+'=>605,','=>238,'-'=>375,'.'=>238,'/'=>334,'0'=>462,'1'=>462,
'2'=>462,'3'=>462,'4'=>462,'5'=>462,'6'=>462,'7'=>462,'8'=>462,'9'=>462,':'=>238,';'=>238,
'<'=>605,'='=>605,'>'=>605,'?'=>344,'@'=>748,'A'=>684,'B'=>560,'C'=>695,'D'=>739,'E'=>563,
'F'=>511,'G'=>729,'H'=>793,'I'=>318,'J'=>312,'K'=>666,'L'=>526,'M'=>896,'N'=>758,'O'=>772,
'P'=>544,'Q'=>772,'R'=>628,'S'=>465,'T'=>607,'U'=>753,'V'=>711,'W'=>972,'X'=>647,'Y'=>620,
'Z'=>607,'['=>374,''=>333,']'=>374,'^'=>606,'_'=>500,'`'=>239,'a'=>417,'b'=>503,'c'=>427,
'd'=>529,'e'=>415,'f'=>264,'g'=>444,'h'=>518,'i'=>241,'j'=>230,'k'=>495,'l'=>228,'m'=>793,
'n'=>527,'o'=>524,'p'=>524,'q'=>504,'r'=>338,'s'=>336,'t'=>277,'u'=>517,'v'=>450,'w'=>652,
'x'=>466,'y'=>452,'z'=>407,'{'=>370,'|'=>258,'}'=>370,'~'=>605);

class PDF_Chinese extends FPDF
{
function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
{
$i=count($this->fonts)+1;
$fontkey=strtolower($family).strtoupper($style);
$this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-120,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
}

function AddBig5Font($family='Big5')
{
$cw=$GLOBALS['Big5_widths'];
$name='MSungStd-Light-Acro';
$CMap='ETenms-B5-H';
$registry=array('ordering'=>'CNS1','supplement'=>0);
$this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
$this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
$this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
$this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
}

function AddGBFont($family='GB')
{
$cw=$GLOBALS['GB_widths'];
$name='STSongStd-Light-Acro';
$CMap='GBKp-EUC-H';
$registry=array('ordering'=>'GB1','supplement'=>2);
$this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
$this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
$this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
$this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
}

function GetStringWidth($s)
{
if($this->CurrentFont['type']=='Type0')
return $this->GetMBStringWidth($s);
else
return parent::GetStringWidth($s);
}

function GetMBStringWidth($s)
{
//Multi-byte version of GetStringWidth()
$l=0;
$cw=&$this->CurrentFont['cw'];
$nb=strlen($s);
$i=0;
while($i<$nb)
{
$c=$s[$i];
if(ord($c)<128)
{
$l+=$cw[$c];
$i++;
}
else
{
$l+=1000;
$i+=2;
}
}
return $l*$this->FontSize/1000;
}

function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
{
if($this->CurrentFont['type']=='Type0')
$this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
else
parent::MultiCell($w,$h,$txt,$border,$align,$fill);
}

function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
{
//Multi-byte version of MultiCell()
$cw=&$this->CurrentFont['cw'];
if($w==0)
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
$s=str_replace("\r",'',$txt);
$nb=strlen($s);
if($nb>0 and $s[$nb-1]=="\n"
$nb--;
$b=0;
if($border)
{
if($border==1)
{
$border='LTRB';
$b='LRT';
$b2='LR';
}
else
{
$b2='';
if(is_int(strpos($border,'L')))
$b2.='L';
if(is_int(strpos($border,'R')))
$b2.='R';
$b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
}
}
$sep=-1;
$i=0;
$j=0;
$l=0;
$ns=0;
$nl=1;
while($i<$nb)
{
//Get next character
$c=$s[$i];
//Check if ASCII or MB
$ascii=(ord($c)<128);
if($c=="\n"
{
//Explicit line break
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$i++;
$sep=-1;
$j=$i;
$l=0;
$ns=0;
$nl++;
if($border and $nl==2)
$b=$b2;
continue;
}
if(!$ascii)
{
$sep=$i;
$ls=$l;
}
elseif($c==' ')
{
$sep=$i;
$ls=$l;
$ns++;
}
$l+=$ascii ? $cw[$c] : 1000;
if($l>$wmax)
{
//Automatic line break
if($sep==-1 or $i==$j)
{
if($i==$j)
$i+=$ascii ? 1 : 2;
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
}
else
{
if($align=='J')
{
if($s[$sep]==' ')
$ns--;
if($s[$i-1]==' ')
{
$ns--;
$ls-=$cw[' '];
}
$this->ws=($ns>0) ? ($wmax-$ls)/1000*$this->FontSize/$ns : 0;
$this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
}
$this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
$i=($s[$sep]==' ') ? $sep+1 : $sep;
}
$sep=-1;
$j=$i;
$l=0;
$ns=0;
$nl++;
if($border and $nl==2)
$b=$b2;
}
else
$i+=$ascii ? 1 : 2;
}
//Last chunk
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
if($border and is_int(strpos($border,'B')))
$b.='B';
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$this->x=$this->lMargin;
}

function Write($h,$txt,$link='')
{
if($this->CurrentFont['type']=='Type0')
$this->MBWrite($h,$txt,$link);
else
parent::Write($h,$txt,$link);
}

function MBWrite($h,$txt,$link)
{
//Multi-byte version of Write()
$cw=&$this->CurrentFont['cw'];
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
$s=str_replace("\r",'',$txt);
$nb=strlen($s);
$sep=-1;
$i=0;
$j=0;
$l=0;
$nl=1;
while($i<$nb)
{
//Get next character
$c=$s[$i];
//Check if ASCII or MB
$ascii=(ord($c)<128);
if($c=="\n"
{
//Explicit line break
$this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
$i++;
$sep=-1;
$j=$i;
$l=0;
if($nl==1)
{
$this->x=$this->lMargin;
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
}
$nl++;
continue;
}
if(!$ascii or $c==' ')
$sep=$i;
$l+=$ascii ? $cw[$c] : 1000;
if($l>$wmax)
{
//Automatic line break
if($sep==-1 or $i==$j)
{
if($this->x>$this->lMargin)
{
//Move to next line
$this->x=$this->lMargin;
$this->y+=$h;
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
$i++;
$nl++;
continue;
}
if($i==$j)
$i+=$ascii ? 1 : 2;
$this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
}
else
{
$this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
$i=($s[$sep]==' ') ? $sep+1 : $sep;
}
$sep=-1;
$j=$i;
$l=0;
if($nl==1)
{
$this->x=$this->lMargin;
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
}
$nl++;
}
else
$i+=$ascii ? 1 : 2;
}
//Last chunk
if($i!=$j)
$this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
}

function _putfonts()
{
$nf=$this->n;
foreach($this->diffs as $diff)
{
//Encodings
$this->_newobj();
$this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
$this->_out('endobj');
}
$mqr=get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
foreach($this->FontFiles as $file=>$info)
{
//Font file embedding
$this->_newobj();
$this->FontFiles[$file]['n']=$this->n;
if(defined('FPDF_FONTPATH'))
$file=FPDF_FONTPATH.$file;
$size=filesize($file);
if(!$size)
$this->Error('Font file not found');
$this->_out('<</Length '.$size);
if(substr($file,-2)=='.z')
$this->_out('/Filter /FlateDecode');
$this->_out('/Length1 '.$info['length1']);
if(isset($info['length2']))
$this->_out('/Length2 '.$info['length2'].' /Length3 0');
$this->_out('>>');
$f=fopen($file,'rb');
$this->_putstream(fread($f,$size));
fclose($f);
$this->_out('endobj');
}
set_magic_quotes_runtime($mqr);
foreach($this->fonts as $k=>$font)
{
//Font objects
$this->_newobj();
$this->fonts[$k]['n']=$this->n;
$this->_out('<</Type /Font');
if($font['type']=='Type0')
$this->_putType0($font);
else
{
$name=$font['name'];
$this->_out('/BaseFont /'.$name);
if($font['type']=='core')
{
//Standard font
$this->_out('/Subtype /Type1');
if($name!='Symbol' and $name!='ZapfDingbats')
$this->_out('/Encoding /WinAnsiEncoding');
}
else
{
//Additional font
$this->_out('/Subtype /'.$font['type']);
$this->_out('/FirstChar 32');
$this->_out('/LastChar 255');
$this->_out('/Widths '.($this->n+1).' 0 R');
$this->_out('/FontDescriptor '.($this->n+2).' 0 R');
if($font['enc'])
{
if(isset($font['diff']))
$this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
else
$this->_out('/Encoding /WinAnsiEncoding');
}
}
$this->_out('>>');
$this->_out('endobj');
if($font['type']!='core')
{
//Widths
$this->_newobj();
$cw=&$font['cw'];
$s='[';
for($i=32;$i<=255;$i++)
$s.=$cw[chr($i)].' ';
$this->_out($s.']');
$this->_out('endobj');
//Descriptor
$this->_newobj();
$s='<</Type /FontDescriptor /FontName /'.$name;
foreach($font['desc'] as $k=>$v)
$s.=' /'.$k.' '.$v;
$file=$font['file'];
if($file)
$s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
$this->_out($s.'>>');
$this->_out('endobj');
}
}
}
}

function _putType0($font)
{
//Type0
$this->_out('/Subtype /Type0');
$this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
$this->_out('/Encoding /'.$font['CMap']);
$this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
$this->_out('>>');
$this->_out('endobj');
//CIDFont
$this->_newobj();
$this->_out('<</Type /Font');
$this->_out('/Subtype /CIDFontType0');
$this->_out('/BaseFont /'.$font['name']);
$this->_out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('.$font['registry']['ordering'].') /Supplement '.$font['registry']['supplement'].'>>');
$this->_out('/FontDescriptor '.($this->n+1).' 0 R');
$W='/W [1 [';
foreach($font['cw'] as $w)
$W.=$w.' ';
$this->_out($W.']]');
$this->_out('>>');
$this->_out('endobj');
//Font descriptor
$this->_newobj();
$this->_out('<</Type /FontDescriptor');
$this->_out('/FontName /'.$font['name']);
$this->_out('/Flags 6');
$this->_out('/FontBBox [0 0 1000 1000]');
$this->_out('/ItalicAngle 0');
$this->_out('/Ascent 1000');
$this->_out('/Descent 0');
$this->_out('/CapHeight 1000');
$this->_out('/StemV 10');
$this->_out('>>');
$this->_out('endobj');
}
}
?>

将以上代码存为chinese.php即可引用。但用它只能得到一种字体。为了支持所有中文字体,可用ttf2pt1程序将TrueType字体转化pt1字体,一个一个地转(具体方法在FPDF的教程中有详细说明)。为了支持其他中文字体,养分要修改上面的chinese.php,如下:

1: Replace the following line in the AddGBFont() method:

function AddGBFont($family='GB',$name='STSongStd-Light-Acro')
{
$cw=$GLOBALS['GB_widths'];
// $name='STSongStd-Light-Acro';
$CMap='GBKp-EUC-H';
........

2: This is a Sample.

<?php
require('chinese.php');

$pdf=new PDF_Chinese();
$pdf->AddGBFont('simsun','宋体');
$pdf->AddGBFont('simhei','黑体');
$pdf->AddGBFont('simkai','楷体_GB2312');
$pdf->AddGBFont('sinfang','仿宋_GB2312'');
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('simsun','',20);
$pdf->Write(10,'简体中文汉字‘);
$pdf->SetFont('simhei','',20);
$pdf->Write(10,'简体中文汉字');
$pdf->SetFont('simkai','',20);
$pdf->Write(10,'简体中文汉字‘);
$pdf->SetFont('sinfang','',20);
$pdf->Write(10,'简体中文汉字');
$pdf->Output();
?>

 


    
[2]PHP与javascript对多项选择的处理
    来源: 互联网  发布时间: 2013-11-30

  我们经常要给用户作出多项选择进行处理,例如允许用户对列表项选择多项后删除选定项等。今天举个例子说明PHP和JavaScript分别是怎样处理多项选择的。今天我们做的是一个投票系统,对MySQL数据库itemtable表里的项目进行投票,且每个别IP能且只能投两票。



  表itemtable是通过以下MySQL语句建立的:

CREATE TABLE `itemtable` (
`id` TINYINT( 4 ) NOT NULL AUTO_INCREMENT,
`name` VARCHAR( 50 ) NOT NULL ,
`votes` SMALLINT( 6 ) NOT NULL ,
PRIMARY KEY ( `id` )
);

其中字段“name”是列表项目名,“votes”是所得票数。我们还要建立一个表“voteiptable”来记录投票用户的IP:

CREATE TABLE `voteiptable` (
`id` SMALLINT( 6 ) NOT NULL ,
`voteip` VARCHAR( 15 ) NOT NULL,
PRIMARY KEY ( `id` )
);

下面我们编写文件“multivote.php”,今天我们要用到一个数据库类文件“dbclass.php”.




  我们可以发现,客户端的JavaScript和服务器端的PHP对多项选择的处理有很多相似之处,当然也有不同。这是比较经典的多选项处理的程序,如果不限定用户的选项的话就更简单了。


    
[3]3
    来源: 互联网  发布时间: 2013-11-30

{
                    if (is_array($value) && ! $delete)
                    {
                        foreach ($value as $suboption => $subvalue)
                        {
                            $this->{$option}["$suboption"] = $subvalue;
                        }
                    }
                    else
                    {
                          $this->$option = $value;
                    }
                }
            }
        }
    }

    // these are the functions, which are intended to be overriden in user classes

    /**
    *
    * @param    mixed
    * @return   object  DomNode
    * @access   private
    */
    function insertNewResult(&$metadata)
    {
        if ($this->xmlroot)
            return $this->xmlroot->new_child($this->tagNameResult, NULL);
        else
        {
            $this->xmlroot = $this->xmldoc->add_root($this->tagNameResult);
            //PHP 4.0.6 had $root->name as tagname, check for that here...
            if (!isset($this->xmlroot->{$this->tagname}))
            {
                $this->tagname = "name";
            }
            return $this->xmlroot;

        }
    }


    /**
    *   to be written
    *
    * @param    object DomNode $parent_row
    * @param    mixed $res
    * @param    mixed $key
    * @param    mixed &metadata
    * @return   object DomNode
    * @access private
    */
    function insertNewRow($parent_row, $res, $key, &$metadata)
    {
        return  $parent_row->new_child($this->tagNameRow, Null);
    }


    /**
    *   to be written
    *
    * @param    object DomNode $parent
    * @param    mixed $res
    * @param    mixed $key
    * @param    mixed &$metadata
    * @param    mixed &$subrow
    * @return   object DomNode
    * @access private
    */
    function insertNewElement($parent, $res, $key, &$metadata, &$subrow)
    {
        return  $parent->new_child($metadata[$key]["name"], $this->xml_encode(trim$res[$key]));
    }


    /**
    *   to be written
    *
    * @param    mixed $key
    * @param    mixed $value
    * @param    mixed &$metadata
    * @access private
    */
    function addTableInfo($key, $value, &$metadata) {

    }

    // end functions, which are intended to be overriden in user classes

    // here come some helper functions...

    /**
    * make utf8 out of the input data and escape & with & and "< " with "< "
    * (we assume that when there's no space after < it's a tag, which we need in the xml)
    *  I'm not sure, if this is the standard way, but it works for me.
    *
    * @param    string text to be utfed.
    * @access private
    */
    function xml_encode ($text)
    {
        if (function_exists("iconv") && isset($this->encoding_from) && isset($this->encoding_to))
        {
             ini_set("track_errors",1);
             $text = iconv($this->encoding_from,$this->encoding_to,ereg_replace("&","&",ereg_replace("< ","< ",$text)));

             if (! isset($text) )
             {
                if (isset($php_errormsg))
                {
                    $errormsg = "error: $php_errormsg";
                }
                else
                {
                    $errormsg = "undefined iconv error, turn on track_errors in php.ini to get more details";
                }
                return PEAR::raiseError($errormsg,Null,PEAR_ERROR_DIE);
             }
             else {
                return $text;
             }
        }
        else
        {
            //$text = utf8_encode(ereg_replace("&","&",ereg_replace("< ","< ",$text)));
            $text = trim(ereg_replace("&","&",ereg_replace("< ","< ",$text)));
//            echo $text;
        }
        return $text;
    }

    //taken from kc@hireability.com at http://www.php.net/manual/en/function.array-merge-recursive.php
    /**
    * There seemed to be no built in function that would merge two arrays recursively and clobber
    *   any existing key/value pairs. Array_Merge() is not recursive, and array_merge_recursive
    *   seemed to give unsatisfactory results... it would append duplicate key/values.
    *
    *   So here's a cross between array_merge and array_merge_recursive
    **/
    /**
    *
    * @param    array first array to be merged
    * @param    array second array to be merged
    * @return   array merged array
    * @access private
    */
    function array_merge_clobber($a1,$a2)
    {
        if(!is_array($a1)
!is_array($a2)) return false;
        $newarray = $a1;
        while (list($key, $val) = each($a2))
        {
            if (is_array($val) && is_array($newarray[$key]))
            {
                $newarray[$key] = $this->array_merge_clobber($newarray[$key], $val);
            }
            else
            {
                $newarray[$key] = $val;
            }
        }
        return $newarray;
    }

    /**
    * Adds a xml string to $this->xmldoc.
    * It's inserted on the same level as a "normal" resultset, means just as a children of <root>
    * if a xpath expression is supplied, it takes that for selecting only part of the xml-file
    *
    * the clean code works only with php 4.0.7
    * for php4.0.6 :
    * I found no cleaner method than the below one. it's maybe nasty (xmlObject->string->xmlObject),
    *  but it works. If someone knows how to add whole DomNodes to another one, let me know...
    *
    * @param    string xml string
    * @param    mixed xpath  either a string with the xpath expression or an array with "xpath"=>xpath expression  and "root"=tag/subtag/etc, which are the tags to be inserted before the result
    * @access private
    */

    function doXmlString2Xml ($string,$xpath = Null)
    {

        //check if we have a recent domxml. otherwise use the workaround...
        $version = explode(".",phpversion());

        if (! ($version[0] <= 4 and $version[1] <= 0 and $version[2] < 7) ){

            if (is_array($xpath))
            {
                if (isset($xpath["root"]))
                {
                    $root = $xpath["root"];
                }
                $xpath = $xpath["xpath"];
            }

            $tmpxml = xmldoc($string);
            $subroot = $this->xmlroot;

            if (isset($root))
            {
                $roots = explode("/",$root);
                foreach ($roots as $rootelement)
                {
                    if ( strlen($rootelement) > 0 )
                    {
                        $subroot = $subroot->new_child($rootelement,"");
                    }
                }
            }


            //$this->xmlroot->addchild does some strange things when added nodes from xpath.... so this comment helps out
            $newchild = $subroot->add_child($this->xmldoc->create_comment("the purpose of this comment is a workaround in sql2php.php line ".__LINE__));


            // if no xpath is given, just take the whole file
            if ( (is_null($xpath)))
            {
                $newchild->append_child($tmpxml->root());
            }
            else
            {
                $xctx = $tmpxml->xpath_new_context();
                $xnode = xpath_eval($xctx,$xpath);
                foreach ($xnode->nodeset as $node)
                {
                    $newchild->append_child($node);
                }
            }

         }
        else {
            $MainXmlString = $this->xmldoc->dumpmem();
            $string = preg_replace("/<\?xml.*\?>/","",$string);

            $MainXmlString = preg_replace("/<".$this->xmlroot->{$this->tagname}."\/>/","<".$this->xmlroot->{$this->tagname}."></".$this->xmlroot->{$this->tagname}.">",$MainXmlString);
            $MainXmlString = preg_replace("/<\/".$this->xmlroot->{$this->tagname}.">/",$string."</".$this->xmlroot->{$this->tagname}.">",$MainXmlString);

            $this->xmldoc = xmldoc($MainXmlString);
            $this->xmlroot = $this->xmldoc->root();

        }
    }

    /**
    * sets the encoding for the db2xml transformation
    * @param    string $encoding_from encoding to transform from
    * @param    string $encoding_to encoding to transform to
    * @access public
    */
    function setEncoding ($encoding_from = "ISO-8859-1", $encoding_to ="UTF-8")
    {
        $this->encoding_from = $encoding_from;
        $this->encoding_to = $encoding_to;
    }
    /**
    * @param array $parentTables parent to child relation
    * @access public
    */

    function SetParentTables($parentTables)
    {
        foreach ($parentTables as $table => $parent)
        {
            $table_info["parent_table"][$table]=$parent;
        }
        $this->SetOptions(array("user_tableInfo"=>$table_info));
    }


    /**
    * returns the content of the first match of the xpath expression
    *
    * @param    string $expr xpath expression
    * @return   mixed content of the evaluated xpath expression
    * @access   public
    */

    function getXpathValue ($expr)
    {

        $xpth = $this->xmldoc->xpath_new_context();
        $xnode = xpath_eval($xpth,$expr);

        if (isset ($xnode->nodeset[0]))
        {
            $firstnode = $xnode->nodeset[0];

            $children = $firstnode->children();
            $value = $children[0]->content;
                return $value;
        }

        else
        {
            return Null;
        }
    }

    /**
    * get the values as an array from the childtags from the first match of the xpath expression
    *
    * @param    string xpath expression
    * @return   array with key->value of subtags
    * @access   public
    */

    function getXpathChildValues ($expr)
    {
        $xpth = $this->xmldoc->xpath_new_context();
        $xnode = xpath_eval($xpth,$expr);

        if (isset ($xnode->nodeset[0]))
        {
            foreach ($xnode->nodeset[0]->children() as $child)
            {
                $children = $child->children();
                $value[$child->{$this->tagname}] = $children[0]->content;
            }
            return $value;
        }
        else
        {
            return Null;
        }
    }

}
?>

    
最新技术文章:
▪PHP函数microtime()时间戳的定义与用法
编程技术 iis7站长之家
▪PHP数组排序方法总结(收藏)
▪php数组排序方法大全(脚本学堂整理奉献)
▪php数组排序的几个函数(附实例)
▪php二维数组排序(实例)
▪php根据键值对二维数组排序的小例子
▪php验证码(附截图)
▪php数组长度的获取方法(三个实例)
▪php获取数组长度的方法举例
▪判断php数组维度(php数组长度)的方法
▪php获取图片的exif信息的示例代码
▪PHP 数组key长度对性能的影响实例分析
▪php函数指定默认值的方法示例
▪php提交表单到当前页面、提交表单后页面重定...
▪php四舍五入的三种实现方法
▪php获得数组长度(元素个数)的方法
▪php日期函数的简单示例代码
▪php数学函数的简单示例代码
▪php字符串函数的简单示例代码
▪php文件下载代码(多浏览器兼容、支持中文文...
▪php实现文件下载、支持中文文件名的示例代码...
▪php文件下载(防止中文文件名乱码)的示例代码
▪解决PHP文件下载时中文文件名乱码的问题
▪php数组去重(一维、二维数组去重)的简单示例
▪php小数点后取两位的三种实现方法
▪php Redis 队列服务的简单示例
▪PHP导出excel时数字变为科学计数的解决方法
▪PHP数组根据值获取Key的简单示例
▪php数组去重的函数代码示例
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3