Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP & Regex : Replace entire <head>

Posted on 2009-04-22
3
Medium Priority
?
346 Views
Last Modified: 2012-05-06
I'm storing an entire html page in $html. I need to replace everything from <head> to </head> with my own $head (so either the stuff inside is replaced, or the stuff inside and the tags - whichever makes more sense). How can I do this?
0
Comment
Question by:level9wizard
  • 2
3 Comments
 
LVL 6

Expert Comment

by:hampus_b
ID: 24204464
$html = preg_replace("|<head>(.*)</head>|m",$newhead,$html);
0
 
LVL 6

Accepted Solution

by:
hampus_b earned 2000 total points
ID: 24204541
or a little bit better:
<?
$html = '
<html>
<head>
old head
</head>
asdasdasd
';
 
$newhead = "<head>testar</head>";
$html = preg_replace('|<head>.*<\/head>|siu',$newhead,$html);
echo $html;
?>

Open in new window

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

580 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