2012-09-18 3 views
0

href 링크가있는 xml에 문제가 있습니다. 올바른 순서로 출력 할 수 없습니다.PHP - HTML 태그로 XML에서 링크를 구문 분석하는 방법?

는 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 기능 :

XML과 메서드를 호출
<?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는 :

:

<?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); ?> 

결과이된다 17,451,515,

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

그것은해야한다 :

텍스트 [email protected] 텍스트

+0

정말 XML입니까? –

+0

방금 ​​업데이트했습니다 ... 먼저 코드 태그를 사용하는 것을 잊었습니다. – TimTastic

+0

최소한의 작업 예제를 제공해주십시오. XML이 유효하지 않으며 PHP 코드가 단독으로 실행되지 않습니다. – rodion

답변

0

난이 도움이되기를 바랍니다.

<?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 링크 만 가져옵니다. 결과를 얻고 싶습니다. "Some text ** LINK ** Some Text"는 XML에 저장되어있는 것과 비슷합니다.하지만 그 방법을 알지 못합니다. – TimTastic

0

이 링크의 BBCode함으로써이를 해결이 링크를 시도하고 다음을 preg_matched : 내가 SimpleXML을에서 다시 얻을 XML을 극복하기 위해 XPath를 사용하여 즐길 수 링크를 다시합니다.

관련 문제