Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 37
Cookie not unsetting 7 27
PHP Echo with auto submit 8 28
Force PDF to open inline as opposed to dowload 16 21
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

765 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