Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

23 Experts available now in Live!

Get 1:1 Help Now