$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 ครับ