久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='vMK3S'><tr id='vMK3S'><dt id='vMK3S'><q id='vMK3S'><span id='vMK3S'><b id='vMK3S'><form id='vMK3S'><ins id='vMK3S'></ins><ul id='vMK3S'></ul><sub id='vMK3S'></sub></form><legend id='vMK3S'></legend><bdo id='vMK3S'><pre id='vMK3S'><center id='vMK3S'></center></pre></bdo></b><th id='vMK3S'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vMK3S'><tfoot id='vMK3S'></tfoot><dl id='vMK3S'><fieldset id='vMK3S'></fieldset></dl></div>
  • <tfoot id='vMK3S'></tfoot>

        <bdo id='vMK3S'></bdo><ul id='vMK3S'></ul>

    1. <small id='vMK3S'></small><noframes id='vMK3S'>

      <legend id='vMK3S'><style id='vMK3S'><dir id='vMK3S'><q id='vMK3S'></q></dir></style></legend>

      1. 通過 php dom 在 html 片段中通過超鏈接查找和替換

        find and replace keywords by hyperlinks in an html fragment, via php dom(通過 php dom 在 html 片段中通過超鏈接查找和替換關鍵字)
      2. <small id='t5t5a'></small><noframes id='t5t5a'>

          <legend id='t5t5a'><style id='t5t5a'><dir id='t5t5a'><q id='t5t5a'></q></dir></style></legend>

          1. <i id='t5t5a'><tr id='t5t5a'><dt id='t5t5a'><q id='t5t5a'><span id='t5t5a'><b id='t5t5a'><form id='t5t5a'><ins id='t5t5a'></ins><ul id='t5t5a'></ul><sub id='t5t5a'></sub></form><legend id='t5t5a'></legend><bdo id='t5t5a'><pre id='t5t5a'><center id='t5t5a'></center></pre></bdo></b><th id='t5t5a'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='t5t5a'><tfoot id='t5t5a'></tfoot><dl id='t5t5a'><fieldset id='t5t5a'></fieldset></dl></div>
              <tbody id='t5t5a'></tbody>

            • <bdo id='t5t5a'></bdo><ul id='t5t5a'></ul>
              <tfoot id='t5t5a'></tfoot>
                • 本文介紹了通過 php dom 在 html 片段中通過超鏈接查找和替換關鍵字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在嘗試使用 simple_html_dom php 類來創建查找和替換關鍵字的查找和替換函數并將它們替換為關鍵字定義的鏈接,并將關鍵字作為鏈接文本.

                  我如何使用此類在字符串中找到并用 Dexia</a> 替換Dexia",例如<div><p>德克夏銀行的CEO剛剛決定退休.</p></div>?

                  解決方案

                  這有點棘手,但你可以這樣做:

                  $html = <<<HTML<div><p>德克夏銀行的首席執行官<em>已經</em>剛剛決定退休.</p></div>HTML;

                  我添加了一個強調元素只是為了說明它也適用于內聯元素.

                  設置

                  $dom = 新的 DOMDocument;$dom->formatOutput = TRUE;$dom->loadXML($html);$xpath = new DOMXPath($dom);$nodes = $xpath->query('//text()[contains(., "Dexia")]');

                  上面有趣的當然是XPath.它為所有包含針Dexia"的 DOMText 節點查詢加載的 DOM.結果是 DOMNodeList(像往常一樣).

                  替換

                  foreach($nodes as $node) {$link = '<a href="info.php?tag=dexia">Dexia</a>';$replaced = str_replace('Dexia', $link, $node->wholeText);$newNode = $dom->createDocumentFragment();$newNode->appendXML($replaced);$node->parentNode->replaceChild($newNode, $node);}echo $dom->saveXML($dom->documentElement);

                  找到的 $node 將包含 wholeText 的字符串 The CEO of the Dexia bank ,盡管它在 P 中 元素.那是因為 $node 有一個兄弟 DOMElement,重點放在 bank 之后.我將鏈接創建為字符串而不是節點,并用它替換 wholeText 中所有出現的Dexia"(不管詞邊界如何 - 這將是對 Regex 的一個很好的調用).然后我從結果字符串創建一個 DocumentFragment 并用它替換 DOMText 節點.

                  W3C 與 PHP

                  使用DocumentFragement::applyXML() 是一種非標準方法,因為該方法不是 W3C DOM 規范的一部分.

                  如果您想用標準 API 進行替換,您首先必須將 A 元素創建為新的 DOMElement.然后你必須在 DOMTextnodeValue 中找到Dexia"的偏移量,然后將 DOMText 節點拆分為兩個節點位置.從返回的兄弟中移除 Dexia 并在第二個之前插入 Link 元素.對同級節點重復此過程,直到在節點中找不到更多的 Dexia 字符串.以下是針對一次德克夏的處理方法:

                  foreach($nodes as $node) {$link = $dom->createElement('a', 'Dexia');$link->setAttribute('href', 'info.php?tag=dexia');$offset = strpos($node->nodeValue, '德克夏');$newNode = $node->splitText($offset);$newNode->deleteData(0, strlen('Dexia'));$node->parentNode->insertBefore($link, $newNode);}

                  最后是輸出

                  <p><a href="info.php?tag=dexia">Dexia</a>的CEO銀行<em>有</em>剛剛決定退休.</p>

                  I'm trying to use the simple_html_dom php class to create a find and replace function that looks for keywords and replace them by a link to a definition of the keyword, with the keyword as link text.

                  How can i find and replace "Dexia" with <a href="info.php?tag=dexia">Dexia</a> using this class, inside a string such as <div><p>The CEO of the Dexia bank has just decided to retire.</p></div> ?

                  解決方案

                  That's somewhat tricky, but you could do it this way:

                  $html = <<< HTML
                  <div><p>The CEO of the Dexia bank <em>has</em> just decided to retire.</p></div>
                  HTML;
                  

                  I've added an emphasis element just to illustrate that it works with inline elements too.

                  Setup

                  $dom = new DOMDocument;
                  $dom->formatOutput = TRUE;
                  $dom->loadXML($html);
                  $xpath = new DOMXPath($dom);
                  $nodes = $xpath->query('//text()[contains(., "Dexia")]');
                  

                  The interesting thing above is the XPath of course. It queries the loaded DOM for all DOMText nodes containing the needle "Dexia". The result is DOMNodeList (as usual).

                  The replacement

                  foreach($nodes as $node) {
                      $link     = '<a href="info.php?tag=dexia">Dexia</a>';
                      $replaced = str_replace('Dexia', $link, $node->wholeText);
                      $newNode  = $dom->createDocumentFragment();
                      $newNode->appendXML($replaced);
                      $node->parentNode->replaceChild($newNode, $node);
                  }
                  echo $dom->saveXML($dom->documentElement);
                  

                  The found $node will contain the string The CEO of the Dexia bank for wholeText, despite it being inside the P element. That is because the $node has a sibling DOMElement with the emphasis after bank. I am creating the link as a string instead of a node and replace all occurences of "Dexia" (regardless of word boundary - that would be a good call for Regex) in the wholeText with it. Then I create a DocumentFragment from the resulting string and replace the DOMText node with it.

                  W3C vs PHP

                  Using DocumentFragement::applyXML() is a non-standard approach, because the method is not part of the W3C DOM Specs.

                  If you would want to do the replacement with the standard API, you'd first have to create the A Element as a new DOMElement. Then you'd have to find the offset of "Dexia" in the nodeValue of the DOMText and split the DOMText Node into two nodes at that position. Remove Dexia from the returned sibling and insert the Link Element, before the second one. Repeat this procedure with the sibling node until no more Dexia strings are found in the node. Here is how to do it for one occurence of Dexia:

                  foreach($nodes as $node) {
                      $link = $dom->createElement('a', 'Dexia');
                      $link->setAttribute('href', 'info.php?tag=dexia');
                      $offset  = strpos($node->nodeValue, 'Dexia');
                      $newNode = $node->splitText($offset);
                      $newNode->deleteData(0, strlen('Dexia'));
                      $node->parentNode->insertBefore($link, $newNode);
                  }
                  

                  And finally the output

                  <div>
                    <p>The CEO of the <a href="info.php?tag=dexia">Dexia</a> bank <em>has</em> just decided to retire.</p>
                  </div>
                  

                  這篇關于通過 php dom 在 html 片段中通過超鏈接查找和替換關鍵字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  enable SOAP on PHP(在 PHP 上啟用 SOAP)
                  Get received XML from PHP SOAP Server(從 PHP SOAP 服務器獲取接收到的 XML)
                  not a valid AllXsd value(不是有效的 AllXsd 值)
                  PHP SoapClient: SoapFault exception Could not connect to host(PHP SoapClient:SoapFault 異常無法連接到主機)
                  Implementation of P_SHA1 algorithm in PHP(PHP中P_SHA1算法的實現)
                  Sending a byte array from PHP to WCF(將字節數組從 PHP 發送到 WCF)
                    • <legend id='2YHID'><style id='2YHID'><dir id='2YHID'><q id='2YHID'></q></dir></style></legend>
                        <bdo id='2YHID'></bdo><ul id='2YHID'></ul>
                      • <i id='2YHID'><tr id='2YHID'><dt id='2YHID'><q id='2YHID'><span id='2YHID'><b id='2YHID'><form id='2YHID'><ins id='2YHID'></ins><ul id='2YHID'></ul><sub id='2YHID'></sub></form><legend id='2YHID'></legend><bdo id='2YHID'><pre id='2YHID'><center id='2YHID'></center></pre></bdo></b><th id='2YHID'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='2YHID'><tfoot id='2YHID'></tfoot><dl id='2YHID'><fieldset id='2YHID'></fieldset></dl></div>

                        1. <small id='2YHID'></small><noframes id='2YHID'>

                            <tbody id='2YHID'></tbody>

                            <tfoot id='2YHID'></tfoot>
                            主站蜘蛛池模板: 久久综合狠狠综合久久综合88 | 精品国产乱码久久久久久老虎 | 91麻豆精品一区二区三区 | 欧美亚洲视频在线观看 | 亚洲 中文 欧美 日韩 在线观看 | 成人在线视频看看 | 中文字幕在线不卡 | 亚洲成人免费视频 | av网站观看| www天天操 | 精品久久久久久一区二区 | 久久国产精品72免费观看 | 久久国产精品一区二区三区 | 偷拍自拍网址 | 成人夜晚看av | 欧美一级免费 | 一区二区三区四区不卡视频 | 亚洲一区二区三区高清 | 国产日韩欧美一区 | 久久久精选 | 国产精品毛片一区二区三区 | 欧美精品一区二区三区四区 | 精品国产免费人成在线观看 | 免费看啪啪网站 | 日本一区二区影视 | 色中文在线 | 欧美另类视频在线 | 国产精品久久久久久久午夜片 | 九九久久免费视频 | 亚洲天堂av网 | www九色 | a网站在线观看 | 伊人网综合在线观看 | 天天色官网 | 美女在线观看国产 | 亚洲一区二区三区免费在线 | 日韩欧美精品一区 | 成人免费观看男女羞羞视频 | 国产免费黄网 | 国产精品一二区 | 天堂在线中文 |