dsds661730652211 2015-02-28 20:37
浏览 134
已采纳

如何替换html标签内的值?

I want to replace element content with content which is definded in $tagsReplace array but have problem with preg_replace, my current ode:

$tagsReplace = array(
    'header' => 'header',
    'tag1' => 'this is tag1',
    'tag2' => 'this is tag2',
    'tag3' => 'this is tag3',
    'footer' => 'footer',
); 

$content = '
<!DOCTYPE html>
<hthml>
<header data-edit="true" data-tag-id="header"></header>
<div data-edit="true" data-tag-id="tag1"></div>
<div data-edit="true" data-tag-id="tag2"></div>
<div data-edit="true" data-tag-id="tag3"></div>
<footer data-edit="true" data-tag-id="footer"></footer>
</html>
';

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($content);
$xpath = new DomXpath($dom);

foreach ($xpath->query('//*[@data-edit="true"]') as $rowNode) { 
    $tagID = $rowNode->getAttribute('data-tag-id');
    $content = preg_replace('/(<div.*?data-edit="true"[^>]*>)(.*?)(<\/div>)/i', '$1'. $tagsReplace[$tagID] . '$3', $content);
}
echo $content;

What i want is to get output like that:

<!DOCTYPE html>
<hthml>
<header data-edit="true" data-tag-id="header">header</header>
<div data-edit="true" data-tag-id="tag1">this is tag1</div>
<div data-edit="true" data-tag-id="tag2">this is tag2</div>
<div data-edit="true" data-tag-id="tag3">this is tag3</div>
<footer data-edit="true" data-tag-id="footer">footer</footer>
</html>

Current output is:

 <!DOCTYPE html>
<hthml>
<header data-edit="true" data-tag-id="header"></header>
<div data-edit="true" data-tag-id="tag1">footer</div>
<div data-edit="true" data-tag-id="tag2">footer</div>
<div data-edit="true" data-tag-id="tag3">footer</div>
<footer data-edit="true" data-tag-id="footer"></footer>
</html>

Soo all elements which contain atribute data-edit="true" and data-tag-id has to get content inside them replaced by tagid value which is definded in arra $tagsReplace.

  • 写回答

6条回答 默认 最新

  • doublel83422 2015-03-01 00:02
    关注

    Because you are specifying div in your preg_match pattern and we know that the first element starts with header so it won't be matched so simply replace the "div" at the beginning of the preg_match pattern with (.*?) to match header and even other words. this :

    $content = preg_replace('/(<div.?data-edit="true"[^>]>)(.*?)(</div>)/i', '$1'. $tagsReplace[$tagID] . '$3', $content);

    becomes:

    $content = preg_replace('/(<(.*?) data-edit="true"[^>]>)(.?)(</div>)/i', '$1'. $tagsReplace[$tagID] . '$3', $content);

    Another option is available you can use preg_replace_callback(pattern,function,subject) to do what you want magically, here is a solution that worked for me :

    $content = '
    <!DOCTYPE html>
    <hthml>
    <header data-edit="true" data-tag-id="header"></header>
    <div data-edit="true" data-tag-id="tag1"></div>
    <div data-edit="true" data-tag-id="tag2"></div>
    <div data-edit="true" data-tag-id="tag3"></div>
    <footer data-edit="true" data-tag-id="footer"></footer>
    </html>
    ';
    
    $pattern = '/<(.+) data-edit="true" data-tag-id="(.*)"(.*?)>(.*?)<\/(.+)>/';
    $another = preg_replace_callback($pattern, function($matches){
        $tagsReplace = array(
            'header' => 'header1',
            'tag1' => 'this is tag1',
            'tag2' => 'this is tag2',
            'tag3' => 'this is tag3',
            'footer' => 'footer',
        ); 
        return '<'.$matches[1].' data-edit="true" data-tag-id="'.$matches[2].'"'.$matches[3].'>'.$tagsReplace[$matches[2]].'<\/'.$matches[5].'>/';
    }, $content);
    # before the replacement 
    print_r($content);
    echo '
    ';
    // another holds the content after replacement 
    print_r($another);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器