开发者

Find and replace variable div contents

I have a php page which contains a large amount of HTML in it. One part of the HTML has a div in the following format:

<div class="reusable-block" id="xyzabcwy">there is a lot of HTML here which may be in any format</div>

Keep in mind, this div is contained within the DOM at any location however, I do know the div ID programatically.

I was originally finding this string within my database, since a record of it exists there however, the format between the data in the database record and the page are sometimes different due to whitespace but other than the white space, the stri开发者_StackOverflow中文版ngs are exactly the same. The problem is, I don't know what format the whitespace is in.

It seems it is better to write a regular expression to find this div and replace it entirely. I could use a hand though.

Other ideas are also welcome. Many thanks!


If you are using jQuery,

$('#xyzabcwy').html(new_data);

if not

document.getElementById('xyzabcwy').innerHTML = new_data;

otherwise, here is a PHP example.

Edit: PHP

<?php
 $id = "xyzabcwy";
 $html = "<div id=\"" . $id . "\">this is html</div>";
 $newdata = "test";
 echo preg_replace("#<div[^>]*id=\"{$id}\".*?</div>#si",$newdata,$html);
?>

 This should output

<div id="123">test</div>

Answer from: Replace a div content with PHP

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜