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 8330973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:17:08+00:00 2026-06-09T02:17:08+00:00

Consider a document in the following format: <!DOCTYPE html> <html> <head> <title></title> <body> <div

  • 0

Consider a document in the following format:

<!DOCTYPE html>
<html>
<head>
<title></title>
<body>
   <div class="blog_post_item first">
       <?php // some child elements ?>
   </div><!-- end blog_post_item -->
</body>
</html>

I am loading a document like this from one domain to another with PHP cURL. I would like to trim my cURL result to only include div.blog_post_item.first and its children. I know the structure of the other page, yet I can’t edit it. I imagine I can use preg_match to find the opening and closing tags; they will always look the same, including that ending comment.

I have searched for examples/tutorials of screen scraping with cURL/XPath/XSLT/whatever, and its mostly a cyclical rattling off of names of HTML parsing libraries. For that reason, please provide a simple working example. Please do not simply explain that parsing HTML with regex is a potential security vulnerability. Please do not just list libraries and specifications that I should read further into.

I have some simple PHP cURL code:

$ch = curl_init("http://a.web.page.com");
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);

Of course, now $output contains the entire source. How will I get just the contents of that element?

  • 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-06-09T02:17:09+00:00Added an answer on June 9, 2026 at 2:17 am

    If you are sure about the following structure:

    <div class="blog_post_item first">
       WHATEVER
    </div><!-- end blog_post_item -->
    

    AND you are sure the ending-code doesn’t appear in WHATEVER, then you can simply grab it.

    (Note please that I replaced your original PHP with WHATEVER. CURL will only fetch the HTML, and it will contain content, not PHP.)

    You don’t need a regex. You can also do it simply by searching for the wanted strings, like in my example below.

    $curlResponse = '
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <body>
       <div class="blog_post_item first">
           <?php // some child elements ?>
       </div><!-- end blog_post_item -->
    </body>
    </html>';
    
    $startStr = '<div class="blog_post_item first">';
    $endStr = '</div><!-- end blog_post_item -->';
    
    $startStrPos = strpos($curlResponse, $startStr)+strlen($startStr);
    $endStrPos = strpos($curlResponse, $endStr);
    
    $wanted = substr($curlResponse, $startStrPos, $endStrPos-$startStrPos );
    
    echo htmlentities($wanted);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass
Consider the following document <html> <body> This is some content <script type=text/javascript src=verySlowToRespond.js></script> This
Consider following XML document fragment: <Book> <Title>Example</Title> <Content> Some line </Content> <TOC> Again some
Consider the following code: index.html <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01//EN http://www.w3.org/TR/html4/strict.dtd> <html> <head>
Please consider the following non-working HTML document: <html> <body> <form action= method=GET onsubmit=return f(this);>
Consider the following code: <!DOCTYPE html> <html> <body> <script type=text/javascript> var str = '<12>
Consider the following example: <html> <body> <script type=text/javascript> var str=filename.jpg; var pattOne = new
Consider a website installed in the 'mysite' directory: /var/www/html/mysite/index.php document root = /var/www/html url
I am testing against the following test document: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC
Consider the following python class: class Event(Document): name = StringField() time = DateField() location

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.