2012-09-18 46 views
0

我有一個xml有一個href鏈接的問題,我不能以正確的順序回顯。PHP的 - 如何解析從HTML與HTML標籤的鏈接?

XML:

<root> 
     <x>290</x> 
     <y>204</y> 
     <width>420</width> 
     <height>70</height> 
     <htmlText> 
       <TEXTFORMAT LEADING="7"> 
       <P ALIGN="CENTER"> 
       <FONT FACE="Arial" SIZE="12" COLOR="#333333" LETTERSPACING="0" KERNING="0"> 
       SOME TEXT 
       <A HREF="mailto:[email protected]" TARGET=""> 
       <U>[email protected]</U> 
       </A> SOME TEXT 
       </FONT> 
       </P> 
       </TEXTFORMAT> 
       </htmlText> 
    </root> 

我moduletext功能:

<?php 
class modules 
{ 
    private $xml; 
    protected $build; 
    // div text 
    public $div_x, $div_y, $div_width, $div_height, $title, $post, $date, $caption_fontfamily, $caption, $caption_fontsize, $caption_color, $caption_ls, $serverEmail, $name, $email, $message, $src; 

    private $direction, $fontFamily, $af_color, $color, $bold, $italic, $underline, $af_bold, $af_italic, $af_underline, $size; 
    // P 
    public $p_attr_color, $p_attr_align, $p_attr_fontfamily, $p_attr_fontsize, $p_content, $p_content_temp; 
    // Image module 
    private $img_path, $img_x, $img_y, $img_rotation, $img_width, $img_height; 
    // Shape module 
    private $shape_x, $shape_y, $shape_width, $shape_height, $fill_color, $border_color, $border_size, $shape_type, $alpha, $rotation, $prettyPrinting, $opacity; 
    public function moduleText($xml,$print = '') 
    { 
     $this->xml = new SimpleXMLElement($xml); 
     // Plocka ut XML-data 
     $this->div_x = $this->xml->x; 
     $this->div_y = $this->xml->y; 
     $this->div_width = $this->xml->width; 
     $this->div_height = $this->xml->height; 
     $this->divStart = $this->xml->htmlText[0]->TEXTFORMAT->attributes->LEADING; 
     $this->build = '<div id="printthis" style="position:absolute; overflow:auto;left:'.$this->div_x.'px;top:'.$this->div_y.'px;width:'.$this->div_width.'px;height:'.$this->div_height.'px;">'; 

    foreach($this->xml->htmlText as $htmltext) 
     { 
      foreach($htmltext as $textformat) 
      { 
      $line_height = $textformat->attributes()->LEADING; 
       foreach($textformat as $p) 
       { 

        foreach($p as $font) 
        { 

         if(isset($font->A)) 
         {  


         foreach($font->A as $link) { 
          $size = $link->attributes()->SIZE; 
          $target .= $link->attributes()->TARGET; 

          $this->build .= '<div style="width:100%; float:left; margin-left:5px; font-size:'.$size.'px;"><a href="' . str_replace("http://","",$link->attributes()->HREF) . '" target="'.$target.'" style="font-size:'.$size.'px;">'.str_replace(array("http://","mailto:"),"",$link->attributes()->HREF).'</a></div>'; 
          } 
         } 
         $fontsize = $font->attributes()->SIZE; 
         $fontfamily = str_ireplace(array('_'), array(''), $font->attributes()->FACE); 
         $fontcolor = $font->attributes()->COLOR; 
         $test = "1.305"; 
         if(strlen($font) > 0) { 
         $this->build .= '<div align="'.$this->p_attr_align.'" style="width:100%; color:'.$fontcolor.';font-family:'.$fontfamily.';font-size:'.$fontsize.'px; line-height:'.$test.'em;">'; 
         if(isset($font->A)) { 
         $this->build .= iconv('UTF-8','ISO-8859-1', str_ireplace(array('[b]','[/b]','[i]','[/i]'), array('<b>','</b>','<i>','</i>'), $font . '</div>')); 
         } else { 
         $this->build .= iconv('UTF-8','ISO-8859-1', str_ireplace(array('[b]','[/b]','[i]','[/i]'), array('<b>','</b>','<i>','</i>'), $font . '</div>')); 
         } 
         } 
         if(strlen($font->I) > 0) { 
         $this->build .= '<div align="'.$this->p_attr_align.'" style="width:100%; color:'.$fontcolor.';font-family:'.$fontfamily.';font-size:'.$fontsize.'px; line-height:'.$test.'em;">'; 
         $this->build .= iconv('UTF-8','ISO-8859-1', str_ireplace(array('[b]','[/b]','[i]','[/i]'), array('<b>','</b>','<i>','</i>'), "<i>".$font->I . '</i></div>')); 
        } 

        if(strlen($font->U) > 0) { 
         $this->build .= '<div align="'.$this->p_attr_align.'" style="width:100%; text-decoration:underline; color:'.$fontcolor.';font-family:'.$fontfamily.';font-size:'.$fontsize.'px; line-height:'.$test.'em;">'; 

         $this->build .= iconv('UTF-8','ISO-8859-1', str_ireplace(array('[b]','[/b]','[i]','[/i]'), array('<b>','</b>','<i>','</i>'), "<i>".$font->U . '</i>')); 
         $this->build .= '</div>'; 
        } 
        if(strlen($font) == 0 && strlen($font->U) == 0 && strlen($font->I) == 0) { 
        $this->build .= '<div align="'.$this->p_attr_align.'" style="width:100%; text-decoration:underline; color:'.$fontcolor.';font-family:'.$fontfamily.';font-size:'.$fontsize.'px; min-height:'.$test.'em; line-height:'.$test.'em;"></div>'; 
        } 

        } 
       } 
       } 
     } 
     $this->build .= '</div>'; 
       return $this->build; 
    } 
      } 
      ?> 

調用PHP的XML的方法:

<?php include("functions_modules.php"); 
$module = new modules; $xmlcode = '<root> 
<x>290</x> 
<y>204</y> 
<width>420</width> 
<height>70</height> 
<htmlText> 
<TEXTFORMAT LEADING="7"> 
<P ALIGN="CENTER"> 
<FONT FACE="Arial" SIZE="12" COLOR="#333333" LETTERSPACING="0" KERNING="0"> 
SOME TEXT 
<A HREF="mailto:[email protected]" TARGET=""> 
<U>[email protected]</U> 
</A> 
SOME TEXT 
</FONT></P> 
</TEXTFORMAT> 
</htmlText> 
</root>'; 
echo $module->moduleText($xmlcode); ?> 

結果變成這樣:

SOME TEXT 
SOME TEXT 
<a href="mailto:[email protected]">[email protected]</a> 

它應該是:

SOME TEXT [email protected] SOME TEXT

+0

這真的是你的XML嗎? –

+0

我剛更新了它...忘了首先使用codetag – TimTastic

+0

請提供一個最簡單的工作示例。你的XML是無效的,PHP代碼本身並不運行。 – rodion

回答

0

我希望這是有幫助的。我喜歡使用XPath通過XML我回來從SimpleXML來切割:

<?php 
$xml = new SimpleXMLElement("xml_file_path", NULL, True); 
$tags = $xml->xpath('//a'); //use xpath on the XML to find the a tags 

foreach($tags as $tag){ 
    echo $image['href'] ; //here is the a tag src 
} 
?> 

嘗試此鏈接

http://php.net/manual/en/book.simplexml.php

Reading an XML file and store data to mysql database

+0

問題不僅在於獲取鏈接。但要把它放在正確的位置/順序。 – TimTastic

+0

您發佈的xml文件是正確的,它以給定的格式顯示。 –

+0

您發佈的內容只是從xml中取出鏈接href。我想要得到的結果「一些文本** LINK **一些文本」就像它存儲在XML中,但我不知道如何做到這一點。 – TimTastic

0

通過將鏈接設置高亮解決它,然後preg_matched他們再次鏈接。