Jump to content

Replace <div> contents and remove excess code


dingus

Recommended Posts

Ok the problem I am faced with at the moment would seem relatively simple to most people how ever my regex experience is rather limited.

To explain my problem I have the following code

	<div id="playerDiv">
	<div style="padding: 20px; font-size:14px; font-weight: bold;">
		<noscript>Hello, you either have JavaScript turned off or an old version of Adobe's Flash Player. <a href="http://www.macromedia.com/go/getflashplayer/" onclick="_hbLink('Get+Flash','Watch3');">Get the latest Flash player</a>.</noscript>
		<script type="text/javascript">
			document.write('Hello, you either have JavaScript turned off or an old version of Adobe\'s Flash Player. <a href="http://www.macromedia.com/go/getflashplayer/" onclick="_hbLink(\'Get+Flash\',\'Watch3\');">Get the latest Flash player</a>.');
		</script>
	</div>
</div> 
<script type="text/javascript">
	// <![CDATA[
	writeMoviePlayer("playerDiv");
	var to = new SWFObject("/version-check.swf", "checker", "0", "0", "0", "#FFFFFF");
	to.write("checkerDiv");
	// ]]>
</script>

 

What I need to do is replace everything inside the “<div id="playerDiv">” with a variable I have called $code and remove the script tag at the end

 

 

 

Link to comment
Share on other sites

<pre>
<?php
$data = <<<DATA
	<div id="playerDiv">
	<div style="padding: 20px; font-size:14px; font-weight: bold;">
		<noscript>Hello, you either have JavaScript turned off or an old version of Adobe's Flash Player. <a href="http://www.macromedia.com/go/getflashplayer/" onclick="_hbLink('Get+Flash','Watch3');">Get the latest Flash player</a>.</noscript>
		<script type="text/javascript">
			document.write('Hello, you either have JavaScript turned off or an old version of Adobe\'s Flash Player. <a href="http://www.macromedia.com/go/getflashplayer/" onclick="_hbLink(\'Get+Flash\',\'Watch3\');">Get the latest Flash player</a>.');
		</script>
	</div>
</div> 
<script type="text/javascript">
	// <![CDATA[
	writeMoviePlayer("playerDiv");
	var to = new SWFObject("/version-check.swf", "checker", "0", "0", "0", "#FFFFFF");
	to.write("checkerDiv");
	// ]]>
</script>
DATA;
### Split data by begin and end div tags.
$pieces = preg_split('%(?=</?div[^>]*>)%', $data, -1, PREG_SPLIT_NO_EMPTY);
### Strip out playerDiv's content.
$in_div = 0;
$nested = 0;
foreach ($pieces as &$piece) {
	$piece = trim($piece);
	if ($piece == '<div id="playerDiv">') {
		$in_div = 1;
		continue;
	}
	if ($in_div) {
		if (substr($piece, 0, 4) == '<div') {
			++$nested;
		}
		if ($piece == '</div>') {
			--$nested;
			if ($nested == 0) {
				$in_div = 0;
			}
		}
		$piece = '';
	}
}
### Reassemble.
$data = join('', $pieces);
### Clean.
$code = '---code---';
$data = preg_replace('%(?<=<div id="playerDiv">)(?=</div>)%', $code, $data);
$data = preg_replace('%<script[^>]*>.*?</script>%s', '', $data);
echo $data;
?>
</pre> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.