MrMike
V2EX  ›  PHP

PHP 如何比较快速的查找并替换对应的值?

  •  
  •   MrMike · Sep 11, 2017 · 3110 views
    This topic created in 3195 days ago, the information mentioned may be changed or developed.
    原文是:
    <div class="container template-block layout-type" id="template-header" data-template-name="content" data-layout-type="row">
    <div class="row layout-type" data-layout-type="row">
    <div class="col-xs-4 template-column layout-type" data-template-name="content" data-layout-type="column" data-layout-cols="4">
    <div class="template-editor mce-content-body" data-layout-cols-name="name1" id="mce_0" style="position: relative;" spellcheck="false" >
    <p>这里是 NAME1 的值</p>
    </div>
    </div>
    <div class="col-xs-8 template-column layout-type" data-template-name="content" data-layout-type="column" data-layout-cols="8">
    <div class="template-editor mce-content-body" data-layout-cols-name="name2" id="mce_21" style="position: relative;" spellcheck="false" >
    <p>这里是 NAME2 的值</p>
    </div>
    </div>
    </div>
    </div>

    需要替换成:

    <div class="container template-block layout-type" id="template-header" data-template-name="content" data-layout-type="row">
    <div class="row layout-type" data-layout-type="row">
    <div class="col-xs-4 template-column layout-type" data-template-name="content" data-layout-type="column" data-layout-cols="4">
    <div class="template-editor mce-content-body" data-layout-cols-name="name1" id="mce_0" style="position: relative;" spellcheck="false" >{{ name1 }}</div>
    </div>
    <div class="col-xs-8 template-column layout-type" data-template-name="content" data-layout-type="column" data-layout-cols="8">
    <div class="template-editor mce-content-body" data-layout-cols-name="name2" id="mce_21" style="position: relative;" spellcheck="false" >{{ name2 }}</div>
    </div>
    </div>
    </div>

    首先需要查询出整段代码中所有含有 data-layout-cols-name 属性的 div,然后再将其所在的 div 标签内的内容整体替换成一个与 data-layout-cols-name 值相符合的内容,data-layout-cols-name 值不是固定的。

    有没有比较快速的方法?
    3 replies    2017-09-11 17:43:14 +08:00
    dsg003
        1
    dsg003  
       Sep 11, 2017
    300 元,走猪八戒交易平台
    MrMike
        2
    MrMike  
    OP
       Sep 11, 2017
    搞定,结贴,不回复。谢谢。
    eoo
        3
    eoo  
       Sep 11, 2017 via Android
    。。。。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1170 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 17:59 · PVG 01:59 · LAX 10:59 · JFK 13:59
    ♥ Do have faith in what you're doing.