2014-04-25 2 views
0

내 현재 코드에않는 str_replace() DOM

$this->data = $this->result->RetrieveDocumentResult; 

     $this->dom = new DOMDocument(); 
     $this->dom->strictErrorChecking = false; 
     $this->dom->formatOutput = true; 
     $this->dom->loadHTML(base64_decode($this->data)); 

     $exceptions = array(
      'a' => array('href'), 
      'img' => array('src') 
     ); 

     $this->stripAttributes($exceptions); 
     $this->stripSpanTags(); 


     file_put_contents('Recode/' . $flname . '.html', base64_decode($this->data)); 
    } 

    public function stripAttributes(array $exceptions) 
    { 
     $xpath = new DOMXPath($this->dom); 
     if (false === ($elements = $xpath->query("//*"))) die('Xpath error!'); 

     /** @var $element DOMElement */ 
     foreach ($elements as $element) { 
      for ($i = $element->attributes->length; --$i >= 0;) { 
       $this->tag  = $element->nodeName; 
       $this->attribute = $element->attributes->item($i)->nodeName; 

       if ($this->checkAttrExceptions($exceptions)) continue; 

       $element->removeAttribute($this->attribute); 
      } 
     } 

     $this->data = base64_encode($this->dom->saveHTML()); 
    } 

    public function checkAttrExceptions(array $exceptions) 
    { 
     foreach ($exceptions as $tag => $attributes) { 
      if (empty($attributes) || !is_array($attributes)) { 
       die('Attributes not set!'); 
      } 

      foreach ($attributes as $attribute) { 
       if ($tag === $this->tag && $attribute === $this->attribute) { 
        return true; 
       } 
      } 
     } 

     return false; 
    } 


    /** 
    * Strip SPAN tags from current DOM document 
    * 
    * @return void 
    */ 
    /** 
    * Strip SPAN tags from current DOM document 
    * 
    * @return void 
    */ 
    protected function stripSpanTags() 
    { 
     $nodes = $this->dom->getElementsByTagName('span'); 

     while ($span = $nodes->item(0)) { 
      $replacement = $this->dom->createDocumentFragment(); 
      while ($inner = $span->childNodes->item(0)) { 
       $replacement->appendChild($inner); 
      } 
      $span->parentNode->replaceChild($replacement, $span); 
     } 
     $this->data = base64_encode($this->dom->saveHTML()); 

    } 


} 

는 HTML의 모든   다음

$html = str_replace(' ', '', $html); 

했다 그러나 코드의 첫 번째 세트에이를 추가하는 방법과 위치를 혼동 제거 할. 도와주세요

또한 첫 번째 코드 집합의 이전 태그 필터를 덮어 쓰지 않아야합니다.

답변

0

Found Solution., 다음 코드 작동

$this->result = $this->soap->RetrieveDocument(
      array('format' => 'html') 
     ); 

     $this->data = $this->result->RetrieveDocumentResult; 

     $this->dom = new DOMDocument(); 
     $this->dom->strictErrorChecking = false; 
     $this->dom->formatOutput = true; 
     $this->dom->loadHTML(base64_decode($this->data)); 

     $exceptions = array(
      'a' => array('href'), 
      'img' => array('src') 
     ); 

     $this->stripAttributes($exceptions); 
     $this->stripSpanTags(); 

     $decoded = base64_decode($this->data); 
     $decoded = $this->stripNonBreakingSpaces($decoded); 

     file_put_contents('Recode/' . $flname . '.html', $decoded); 
    } 

    public function stripAttributes(array $exceptions) 
    { 
     $xpath = new DOMXPath($this->dom); 
     if (false === ($elements = $xpath->query("//*"))) die('Xpath error!'); 

     /** @var $element DOMElement */ 
     foreach ($elements as $element) { 
      for ($i = $element->attributes->length; --$i >= 0;) { 
       $this->tag  = $element->nodeName; 
       $this->attribute = $element->attributes->item($i)->nodeName; 

       if ($this->checkAttrExceptions($exceptions)) continue; 

       $element->removeAttribute($this->attribute); 
      } 
     } 

     $this->data = base64_encode($this->dom->saveHTML()); 
    } 

    public function checkAttrExceptions(array $exceptions) 
    { 
     foreach ($exceptions as $tag => $attributes) { 
      if (empty($attributes) || !is_array($attributes)) { 
       die('Attributes not set!'); 
      } 

      foreach ($attributes as $attribute) { 
       if ($tag === $this->tag && $attribute === $this->attribute) { 
        return true; 
       } 
      } 
     } 

     return false; 
    } 


    /** 
    * Strip SPAN tags from current DOM document 
    * 
    * @return void 
    */ 
    /** 
    * Strip SPAN tags from current DOM document 
    * 
    * @return void 
    */ 
    protected function stripSpanTags() 
    { 
     $nodes = $this->dom->getElementsByTagName('span'); 

     while ($span = $nodes->item(0)) { 
      $replacement = $this->dom->createDocumentFragment(); 
      while ($inner = $span->childNodes->item(0)) { 
       $replacement->appendChild($inner); 
      } 
      $span->parentNode->replaceChild($replacement, $span); 
     } 

     $this->data = base64_encode($this->dom->saveHTML()); 
    } 


    /** 
    * Replace all   entities within a string with a regular space 
    * 
    * @param string $string Input string 
    * 
    * @return string 
    */ 
    protected function stripNonBreakingSpaces ($string) 
    { 
     return str_replace(' ', ' ', $string); 
    } 


}