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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:01:41+00:00 2026-05-23T01:01:41+00:00

I need of a function in php that extract a description of a site

  • 0

I need of a function in php that extract a description of a site url that don’t have meta tag description any idea?

i have tried this function but don’t work :

$content = file_get_contents($url);

function getExcerpt($content) {
  $text = html_entity_decode($content);
  $excerpt = array();
  //match all tags
  preg_match_all("|<[^>]+>(.*)]+>|", $text, $p, PREG_PATTERN_ORDER);
  for ($x = 0; $x < sizeof($p[0]); $x++) {
    if (preg_match('< p >i', $p[0][$x])) {
      $strip = strip_tags($p[0][$x]);
      if (preg_match("/\./", $strip))
        $excerpt[] = $strip;
    }
    if (isset($excerpt[0])){
      preg_match("/([^.]+.)/", $strip,$matches);
      return $matches[1];
    }
  }
  return false;
}

$excerpt = getExcerpt($content);
  • 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-23T01:01:42+00:00Added an answer on May 23, 2026 at 1:01 am

    Parsing HTML with RegEx is almost always a bad idea. Thankfully PHP has libraries that can do the work for you. The following code uses DOMDocument to extract either the meta description or if one does not exist, the first 1000 characters in the page.

    <?php
    function getExcerpt($html) {
    
        $dom = new DOMDocument();
    
        // Parse the inputted HTML into a DOM
        $dom->loadHTML($html);
    
        $metaTags = $dom->getElementsByTagName('meta');
    
        // Check for a meta description and return it if it exists
        foreach ($metaTags as $metaTag) {
            if ($metaTag->getAttribute('name') === "description") {
                return $metaTag->getAttribute('content');
            }
        }
    
        // No meta description, extract an excerpt from the body
        // Get the body node
        $body = $dom->getElementsByTagName('body');
        $body = $body->item(0);
    
        // extract the contents
        $bodyText = $body->textContent;
    
        // collapse any line breaks
        $bodyText = preg_replace('/\s*\n\s*/', "\n", $bodyText);
        // collapse any more leftover spaces or tabs to single spaces
        $bodyText = preg_replace('/[    ]+/', ' ', $bodyText);
    
        // return the first 1000 chars
        return trim(substr($bodyText, 0, 1000));
    
    }
    
    $html = file_get_contents('test.html');
    
    echo nl2br(getExcerpt($html));
    

    You’ll probably want to add a little more logic to it, some DOM traversal to try to find the content, or just some snippet near the middle of the text. As it is, this code will probably grab a bunch of unwanted stuff like the top of the page navigation etc.

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

Sidebar

Related Questions

I need a regex or a function in PHP that will validate a string
I need a simple proxy PHP function/script that can download a file from a
i need help with disk_total_space function.. i have this on my code <?php $sql=select
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
In PHP Consider this function: function test($name, $age) {} I need to somehow extract
I need a function in php that can generate a 5 char long string
I need a function in php that will calculate the business week in a
I need a php function that can randomly select one row that has a
in function need key to encrypt string without mcrypt libraly in php function encrypt($str,
This question is regarding getopt function in php. I need to pass two parameter

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.