Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6657273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:44:50+00:00 2026-05-26T01:44:50+00:00

yes, I know that using regular expressions on html is not preferred, but I

  • 0

yes, I know that using regular expressions on html is not preferred, but I am still confused as to why this doesn’t work:

I’m trying to remove the “head” from a document.
Here’s the doc:

<html>
 <head>
   <!--
     a comment within the head
     -->
 </head>
 <body>
stuff in the body
 </body>
</html>

My code:

$matches = array(); $result = preg_match ('/(?:<head[^>]*>)(.*?)(<\/head>)/is', $contents, $matches); 
var_dump ($matches);

This does not actually work.
Here’s the output I see:

array(3) { [0]=> string(60) " " [1]=> string(47) " " [2]=> string(7) "" }

However, if I adjust the HTMl doc to not have the comment

What am I missing?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:44:51+00:00Added an answer on May 26, 2026 at 1:44 am

    Your script is working fine, it’s not displaying correctly due to the HTML in the dump (you can tell by the lengths in your var_dump output). Try:

    $result = preg_match ('/(?:<head[^>]*>)(.*?)(<\/head>)/is', $contents, $matches); 
    ob_start(); // Capture the result of var_dump
    var_dump ($matches);
    echo htmlentities(ob_get_clean()); // Escape HTML in the dump
    

    Also, as has been said, you need to use preg_replace to replace the match with '' in order to actually remove the head.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes, I know, I know, parsing HTML with regular expressions is very bad. But
I know that designing state machine generators for regular expressions is not trivial, but
I know that this question has been asked over and over again but still
(Yes I know I can call Java code from Scala; but that is pointless;
First, yes I know about this question , but I'm looking for a bit
I've done this using a regular .cs file that just extends System.Web.UI.UserControl and then
I'm trying to connect to a web service using IO::Socket::INET (yes, I know that
I know that we can protect .Net applications from decompiling by using obfuscators. But
Yes, I'm still using Classic ASP. I'm about to write a script that checks
Yes I know that it shouldn't be abused and that C# is primariy used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.