หัวข้อ: แทนที่แค่ตัวแรกใน DOM ยังไงครับ
เริ่มหัวข้อโดย: affsom ที่ 07 มีนาคม 2017, 21:43:09
$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); }
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'); //$title2 = $title; $keywordOffset = mb_strpos($node->nodeValue, $title, 0, 'UTF-8'); $newNode = $node->splitText($keywordOffset); $newNode->deleteData(0, mb_strlen($title, 'UTF-8')); $span = $dom->createElement('a', $title); $span->setAttribute('target', $target);
if ($link){ $span->setAttribute('href', $link); } elseif ($common_link) { $span->setAttribute('href', $common_link); } else { $span->setAttribute('href', '#'); $span->setAttribute('style', 'text-decoration:none'); } 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); } }
จากโค๊ค 2 โค๊ด ด้านบน มีปัญหาในส่วน $title ที่ต้องการแทนที่แค่อันแรก array 0 1.จะทำการเรียก $title มาจาก Database 2.หากมีคำที่เหมือนกับ $title จะทำการสร้าง link จาก $link (โดยคำที่จะสร้างคือคำที่เหมือนกับ $title) แต่ว่าผมต้องการให้สร้าง link แค่คำแรกที่เหมือนกับ $title ต้องกำหนดอาเรย์ ยังไงครับ ตอนนี้คำใหนที่เหมือนมันใส่$linkให้หมดเลย มือใหม่ php ครับ(https://www.mx7.com/i/195/4eUSKC.png) :wanwan017:
หัวข้อ: Re: แทนที่แค่ตัวแรกใน DOM ยังไงครับ
เริ่มหัวข้อโดย: affsom ที่ 08 มีนาคม 2017, 12:06:00
ทำโมดูล opencart
หัวข้อ: Re: แทนที่แค่ตัวแรกใน DOM ยังไงครับ
เริ่มหัวข้อโดย: sputtaro ที่ 08 มีนาคม 2017, 12:24:13
1. count array 2. loop จะใช้ for วนลูปก็ได้ ง่ายดี 3. ในลูป ถ้า array มากกว่า array[0] ให้ exit คงพอเข้าใจนะครับ
หัวข้อ: Re: แทนที่แค่ตัวแรกใน DOM ยังไงครับ
เริ่มหัวข้อโดย: affsom ที่ 08 มีนาคม 2017, 12:30:10
1. count array 2. loop จะใช้ for วนลูปก็ได้ ง่ายดี 3. ในลูป ถ้า array มากกว่า array[0] ให้ exit คงพอเข้าใจนะครับ
++ Fatal error: Cannot use object of type DOMText as array ลอง count array แล้วมัน error เลยงงครับว่ามันต้อง count ตรงใหน ขอบพระคุณมากครับ อยากขอตัวอย่างสักนิดได้ใหมครับ :wanwan017: :wanwan017:
หัวข้อ: Re: แทนที่แค่ตัวแรกใน DOM ยังไงครับ
เริ่มหัวข้อโดย: sputtaro ที่ 08 มีนาคม 2017, 12:57:21
ผมเพิ่งดูโค๊ดแบบละเอียด
กระบวนการเปลี่ยน title เป็นลิ้งค์ของคุณ น่าจะเปลี่ยนในช่วง
foreach ($pTexts as $pText) { $this->parseText($pText, $title, $dom, $link, $common_link, $target, $description); }
อาจลองแบบนี้ดูครับ
print_r(pTexts); count $pTexts for ... if $pTexts[0] $this->parseText($pTexts[0], $title, $dom, $link, $common_link, $target, $description); else{ exit
ประมาณนี้ครับ หวังว่าจะใช่นะครับ
******************* สั่ง print_r(pTexts); เพื่อดูผลด้วยก็ดีครับ จะได้รู้ว่า มีอะไรอยู่ในนั้นบ้าง
|