Solved

PHP: Put code after </html> before </body>

Posted on 2011-02-24
5
393 Views
Last Modified: 2012-05-11
Using PHP (and maybe REGEX), I want to move all text after </html> and put it before </body>
<?php 

$str = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
<script type="text/javascript">
/*<![CDATA[*/
alert('Hi');
/*]]>*/
</script>
';

$str = fixEnding($str);

function fixEnding($str) {
return $str; // Should everything after </html> to before </body>
}

?>

Open in new window

0
Comment
Question by:hankknight
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:svgmuc
ID: 34973021
Why $str?

str=preg_replace('/<\/body>\n<\/html>(.*)$/''</body>\1</html>',str);
0
 
LVL 14

Assisted Solution

by:svgmuc
svgmuc earned 150 total points
ID: 34973034
Umm.. because it's PHP... I need sleep.

$str=preg_replace('/<\/body>\n<\/html>(.*)$/''</body>\1</html>',$str);

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34973042
If this is yout test.php file, it can be done like this:

<?php
    $f = file_get_contents("test.php");

    $array = explode('</html>',$f);
   
    $pos = strpos($f,'</body>');
    $combine = substr($f,0,$pos).$array[1]."\n</body></html>";
    echo"$combine";
?>

Open in new window

0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 100 total points
ID: 34973090
<?php
    $file = "test.php";
    $content = file_get_contents($file);
    $array = explode('</html>',$content);
    $pos = strpos($content,'</body>');
    $combine = substr($content,0,$pos).$array[1]."\n</body>\n</html>";
    file_put_contents($file,$combine);
?>

Open in new window

0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 34974061
svgmuc's solution has the right idea, but should have the "s" pattern modifier (for the .* to capture multiple lines) and a bit more flexibility in the code structure (in case it differs from the example):

$str=preg_replace("@</body>(.*)</html>(.*)$@s","$2</body>$1</html>",$str);
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now