示例#1
0
 /**
  * @name _parseWrNode
  * @desc Gets a list of run attributes for a WR node
  * @param DOMELEMENT $wr
  * @return $parsedWR array
  */
 private function _parseWrNode($wr)
 {
     $bold = false;
     $italic = false;
     $tab = false;
     $underline = false;
     $text = '';
     $runStyleQuery = $this->xPath->query("w:rPr", $wr);
     # Get inline Styles (italic / bold / underline)
     if ($runStyleQuery->length != 0) {
         $runStyleNode = $runStyleQuery->item(0);
         foreach ($runStyleNode->childNodes as $styleSub) {
             switch ($styleSub->nodeName) {
                 case 'w:i':
                     $italic = true;
                     break;
                 case 'w:b':
                     $bold = true;
                     break;
                 case 'w:u':
                     $underAttr = $styleSub->getAttribute('w:val');
                     if ($underAttr == 'single') {
                         $underline = true;
                     }
                     break;
             }
         }
     }
     # Find if this run is a tab
     $tabQuery = $this->xPath->query("w:tab", $wr);
     if ($tabQuery->length == 1) {
         $tab = true;
     }
     # Get text & escape
     $textQuery = $this->xPath->query("w:t", $wr);
     foreach ($textQuery as $textRes) {
         $text .= $textRes->nodeValue;
     }
     if ($text != '') {
         $text = htmlEntitiesEncode($text);
     }
     $parsedWR = array('bold' => $bold, 'italic' => $italic, 'tab' => $tab, 'underline' => $underline, 'text' => $text);
     return $parsedWR;
 }
示例#2
0
文件: Node.php 项目: lmerotta/docx
 /**
  * @name _parseWrNode
  * @desc Gets a list of run attributes for a WR node
  * @param DOMELEMENT $wr
  * @return $parsedWR array
  */
 private function _parseWrNode($wr)
 {
     $bold = false;
     $italic = false;
     $tab = false;
     $underline = false;
     $text = '';
     $runStyleQuery = $this->xPath->query("w:rPr", $wr);
     # Get inline Styles (italic / bold / underline)
     if ($runStyleQuery->length != 0) {
         $runStyleNode = $runStyleQuery->item(0);
         foreach ($runStyleNode->childNodes as $styleSub) {
             switch ($styleSub->nodeName) {
                 case 'w:i':
                     $italic = true;
                     break;
                 case 'w:b':
                     $bold = true;
                     break;
                 case 'w:u':
                     $underAttr = $styleSub->getAttribute('w:val');
                     if ($underAttr == 'single') {
                         $underline = true;
                     }
                     break;
             }
         }
     }
     # Find if this run is a tab
     $tabQuery = $this->xPath->query("w:tab", $wr);
     if ($tabQuery->length == 1) {
         $tab = true;
     }
     # Get text & escape
     $textQuery = $this->xPath->query("w:t", $wr);
     foreach ($textQuery as $textRes) {
         $text .= $textRes->nodeValue;
     }
     # Load hyperlink data (if any)
     if ($wr->tagName == 'w:hyperlink') {
         $hyperNode = $wr;
         $hyperlink = '';
         foreach ($hyperNode->childNodes as $cn) {
             if ($cn->nodeName == 'w:r') {
                 $hyperlink .= $cn->nodeValue;
             }
         }
         // get relid
         $relid = $hyperNode->attributes->item(0)->nodeValue;
         //get link
         if (!empty($this->docx->relations[$relid]->item(2)->value)) {
             $modHyperlink = $this->docx->relations[$relid]->item(2)->value;
         }
         # If we have the raw hyperlink, parse it
         if ($hyperlink != '') {
             if ($modHyperlink == '') {
                 if (substr($hyperlink, 0, 4) != 'http') {
                     if (strpos($hyperlink, '@') !== false) {
                         $modHyperlink = 'mailto:' . $hyperlink;
                     } else {
                         $modHyperlink = 'http://' . $hyperlink;
                     }
                 } else {
                     $modHyperlink = $hyperlink;
                 }
             }
             $text = '<a href="' . $modHyperlink . '">' . $hyperlink . '</a>';
             $underline = false;
             $tab = false;
             $italic = false;
             $bold = false;
         }
     }
     if ($text != '' && $wr->tagName != 'w:hyperlink') {
         $text = htmlEntitiesEncode($text);
     }
     $endNoteQuery = $this->xPath->query("w:footnoteReference", $wr);
     if ($endNoteQuery->length > 0) {
         foreach ($endNoteQuery as $endNote) {
             /** @var \DOMElement $endNote */
             $text = ' [' . $endNote->attributes->item(0)->nodeValue . ']' . $text;
         }
     }
     $brQuery = $this->xPath->query("w:br", $wr);
     if ($brQuery->length > 0) {
         foreach ($brQuery as $br) {
             $text = '<br>' . $text;
         }
     }
     $parsedWR = array('bold' => $bold, 'italic' => $italic, 'tab' => $tab, 'underline' => $underline, 'text' => $text);
     return $parsedWR;
 }