dingus Posted March 17, 2008 Share Posted March 17, 2008 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted March 17, 2008 Share Posted March 17, 2008 <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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.