Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-24
5
Medium Priority
?
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 600 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 400 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 1000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

688 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