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
https://forums.phpfreaks.com/topic/96483-replace-contents-and-remove-excess-code/
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> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.