how to replace only array[0] in dom (from auto link seo)

Тема в разделе "OpenCart", создана пользователем affgon, 8 мар 2017.

  1. affgon

    affgon

    Регистрация:
    19 янв 2014
    Сообщения:
    125
    Симпатии:
    412
    how to replace only array[0] in dom (from auto link seo)

    i create module from auto link seo pack pro.
    it can use multi language and deferent link base on language or use common link if empty
    all it OK.

    but i have a problem.
    how to set link to only the array[0] of word to link only?

    ===============================
    sorry i'm not RU. and not Eng.
    PHP:
                $this->load->model('cofunction/tooltip');
                    
                
    $tooltips $this->model_cofunction_tooltip->getTooltip();
                    
                if (isset(
    $tooltips) && (strpos($this->data['description'], 'iframe') == false) && (strpos($this->data['description'], 'object') == false)){
                    
    $xdescription mb_convert_encoding(html_entity_decode($this->data['description'], ENT_COMPAT"UTF-8"), 'HTML-ENTITIES'"UTF-8");                     
                    
    libxml_use_internal_errors(true);
                    
    $dom = new DOMDocument;             
                    
    $dom->loadHTML('<div>'.$xdescription.'</div>');               
                    
    libxml_use_internal_errors(false);
                    
                    
    $xpath = new DOMXPath($dom);                           
                    foreach (
    $tooltips as $tooltip) {   
                        
    $common_link $tooltip['common_link'];
                        
    $status $tooltip['status'];
                        
    $target $tooltip['target'];
                        
    $title $tooltip['title'];
                        
    $link mb_convert_encoding(html_entity_decode($tooltip['link'], ENT_COMPAT"UTF-8"), 'HTML-ENTITIES'"UTF-8");                   
                        
    $description html_entity_decode($tooltip['description'], ENT_QUOTES'UTF-8');
                                                
                        
    $pTexts $xpath->query(
                            
    sprintf('///text()[contains(., "%s")]'$title)
                        );
                        
                        foreach (
    $pTexts as $pText) {
                            
    $this->parseText($pText$title$dom$link$common_link$target$description);
                        }

                                    
                    }
                            
                    
    $this->data['description'] = $dom->saveXML($dom->documentElement);               
                }
    PHP:
        private function parseText($node$title$dom$link$common_link$target $description) {               

           
            if (
    mb_strpos($node->nodeValue$title) !== false) {
                
    $this->document->addScript('catalog/view/javascript/tooltip/jquery.cluetip.js');
                
    $this->document->addStyle('catalog/view/javascript/tooltip/jquery.cluetip.css');           
               
                
    $keywordOffset mb_strpos($node->nodeValue$title0'UTF-8');           
                
    $newNode $node->splitText($keywordOffset);
                
    $newNode->deleteData(0mb_strlen($title'UTF-8'));
               
                
    $span $dom->createElement('a'$title);
                
    $span->setAttribute('target'$target);

                if (
    $link){
                    
    $span->setAttribute('href'$link);
                    
    $span->setAttribute('style''text-decoration:underline');
                } elseif (
    $common_link)  {
                    
    $span->setAttribute('href'$common_link);
                    
    $span->setAttribute('style''text-decoration:underline');
                } else {
                    
    $span->setAttribute('style''text-decoration:none');
                    
    $span->removeAttribute('target');
                }
               
                if (
    $description){
                    
    $span->setAttribute('class''title tooltip-link');
                    
    $span->setAttribute('title'$title.'|'.$description);
                }

                
    $node->parentNode->insertBefore($span$newNode);
                
    $this->parseText($newNode ,$title$dom$link$common_link$target$description);
            }                   
        }


    [​IMG]
     
  2. Baco

    Baco Антихронофаг Команда форума

    Регистрация:
    9 окт 2012
    Сообщения:
    803
    Симпатии:
    399
    hi, bro... where are you array to extract [0] index (name) ?

    as example:
    Код:
    foreach ($pTexts as $pText) {
             $this->parseText($pText, $title, $dom, $link, $common_link, $target, $description);
    }
    to extract first element, change foreach to $pTexts[0]... example:
    Код:
    //foreach ($pTexts as $pText) {
             $this->parseText($pTexts[0], $title, $dom, $link, $common_link, $target, $description);
    //}
     
    affgon нравится это.
  3. affgon

    affgon

    Регистрация:
    19 янв 2014
    Сообщения:
    125
    Симпатии:
    412
    i changed but it Fatal error: Cannot use object of type DOMNodeList as array at.

    error at this line ====> $this->parseText($pTexts[0], $title, $dom, $link, $common_link, $target, $description);

    thank.
     
  4. Baco

    Baco Антихронофаг Команда форума

    Регистрация:
    9 окт 2012
    Сообщения:
    803
    Симпатии:
    399
    try:

    Код:
    //foreach ($pTexts as $pText) {
            $this->parseText($pTexts->item(0), $title, $dom, $link, $common_link, $target, $description);
    //}
    
     
    affgon нравится это.
  5. affgon

    affgon

    Регистрация:
    19 янв 2014
    Сообщения:
    125
    Симпатии:
    412
    Thank. But it not solve.
    this file
    in vqmod i do not create link to admin access. must use index.php?route=cofunction/tooltip to access
    Thank

     
    Последнее редактирование: 8 мар 2017
  6. affgon

    affgon

    Регистрация:
    19 янв 2014
    Сообщения:
    125
    Симпатии:
    412
    PHP not solve. I Use javascript for solve

    Код:
    let tracker = {}; // track href values
    
    $('a').each(function(anc) {
      var href = $(this).attr('href');
      if (tracker.hasOwnProperty(href)) {
        $(this).replaceWith(`<div>${$(this).text()}</div>`);
      } else {
        tracker[href] = true;
      }
    });
     
    Lasted edited by : 15 мар 2017