pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<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="6uueseq" 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="iwsagco" 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="0swmswu" 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>
                            主站蜘蛛池模板: 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 匀胶机旋涂仪-声扫显微镜-工业水浸超声-安赛斯(北京)科技有限公司 | 鲸鱼视觉 -数字展厅多媒体互动展示制作公司| 骨龄仪_骨龄检测仪_儿童骨龄测试仪_品牌生产厂家【品源医疗】 | 地磅-电子地磅维修-电子吊秤-汽车衡-无人值守系统-公路治超-鹰牌衡器 | 蓝莓施肥机,智能施肥机,自动施肥机,水肥一体化项目,水肥一体机厂家,小型施肥机,圣大节水,滴灌施工方案,山东圣大节水科技有限公司官网17864474793 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 恒湿机_除湿加湿一体机_恒湿净化消毒一体机厂家-杭州英腾电器有限公司 | 机制砂选粉机_砂石选粉机厂家-盐城市助成粉磨科技有限公司 | 大立教育官网-一级建造师培训-二级建造师培训-造价工程师-安全工程师-监理工程师考试培训 | IP检测-检测您的IP质量 | 南京兰江泵业有限公司-水解酸化池潜水搅拌机-絮凝反应池搅拌机-好氧区潜水推进器 | 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn 锂辉石检测仪器,水泥成分快速分析仪-湘潭宇科分析仪器有限公司 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | 学生作文网_中小学生作文大全与写作指导 | UV-1800紫外光度计-紫外可见光度计厂家-翱艺仪器(上海)有限公司 | 冷凝锅炉_燃气锅炉_工业燃气锅炉改造厂家-北京科诺锅炉 | 生态板-实木生态板-生态板厂家-源木原作生态板品牌-深圳市方舟木业有限公司 | 懂研帝_专业SCI论文润色机构_SCI投稿发表服务公司 | 登车桥动力单元-非标液压泵站-非标液压系统-深圳市三好科技有限公司 | 双舌接地线-PC68数字式高阻计-ZC36|苏海百科 | 西门子气候补偿器,锅炉气候补偿器-陕西沃信机电工程有限公司 | 二维运动混料机,加热型混料机,干粉混料机-南京腾阳干燥设备厂 | 金属检测机_金属分离器_检针验针机_食品药品金属检探测仪器-广东善安科技 | QQ房产导航-免费收录优秀房地产网站_房地产信息网 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 刚性-柔性防水套管-橡胶伸缩接头-波纹管补偿器-启腾供水材料有限公司 | 不锈钢水管-不锈钢燃气管-卫生级不锈钢管件-不锈钢食品级水管-广东双兴新材料集团有限公司 | 巨野月嫂-家政公司-巨野县红墙安康母婴护理中心 | 超声波反应釜【百科】-以马内利仪器 | LHH药品稳定性试验箱-BPS系列恒温恒湿箱-意大利超低温冰箱-上海一恒科学仪器有限公司 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 楼承板设备-楼承板成型机-免浇筑楼承板机器厂家-捡来 | 我爱古诗词_古诗词名句赏析学习平台| 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 选矿设备-新型重选设备-金属矿尾矿重选-青州冠诚重工机械有限公司 | 南汇8424西瓜_南汇玉菇甜瓜-南汇水蜜桃价格 | 深圳公司注册-工商注册代理-注册公司流程和费用_护航财税 | 3d可视化建模_三维展示_产品3d互动数字营销_三维动画制作_3D虚拟商城 【商迪3D】三维展示服务商 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 即用型透析袋,透析袋夹子,药敏纸片,L型涂布棒-上海桥星贸易有限公司 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 |