Solved

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

Posted on 2011-02-24
5
401 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

777 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