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

  • Home
  • SEARCH
  • 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 9088827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:59:31+00:00 2026-06-16T21:59:31+00:00

I’m a beginning programmer trying to make a simple application that just scrapes a

  • 0

I’m a beginning programmer trying to make a simple application that just scrapes a website and returns values.

I’m trying to do something I thought would be simple, but after searching and trying, have given up to just ask.

With my scraper, I return three variables: $title1, $title2, and $title3. All of the $title’s come from different methods of me trying to find the name of the article. Ideally, I’d just have to look for one and be done, but some websites store data differently (some through meta tags, hidden divs, elements, etc).

I need a way to do the following pseudo code:

if $title1, $title2, $title3 != null { // don't count a string if it is null

    $title1_stringlength = string_length($title1) //find string length of the $titles
    $title2_stringlength = string_length($title2)
    $title3_stringlength = string_length($title3)

    $realtitle = $lowestvalueofstringlength; // $realtitle gets whichever $title is shortest in length, not counting any null $title's

}

Here’s an example of why I need to do this:

echo $title1; //echoes "Exercise Daily"
echo $title2; //echoes "null"
echo $title3; //echoes "Exercise Daily - And More advice on SaveTheTwinkie.org"
$realtitle = $title1;//should be $title1 because it was shortest that wasn't null

//or a different example from another site

echo $title1; //echoes "Wow look at this Article Title!"
echo $title2; //echoes "null"
echo $title3; //echoes "Wow look at this Article Title! - from StupidArticles.tv"
$realtitle = $title1;//should be $title1 because it was shortest that wasn't null

So my code would look for the shortest $title in string length (that wasn’t null) and give the value to $realtitle.

Thanks for any and all help! If you need more details, just ask!


EDIT


heres my complete code: It works until one of the $title’s is “”, then $realtitle becomes “” as well

<?php

$sites_html = file_get_contents($url);

$html = new DOMDocument();
@$html->loadHTML($sites_html);
$title1 = null; //reset
$title2 = null; //reset
$title3 = null; //reset

//Get all meta tags and loop through them.
foreach($html->getElementsByTagName('meta') as $meta) {
    if($meta->getAttribute('property')=='og:title'){ 
        //Assign the value from content attribute to $title1
        $title1 = $meta->getAttribute('content');
    }
}
foreach($html->getElementsByTagName('h1') as $div) {
    if($div->getAttribute('itemprop')=='name'){
        $title2 = $div->nodeValue;
    }
}
foreach($html->getElementsByTagName('h1') as $div) {
    if($div->getAttribute('class')=='fn'){
        $title3 = $div->nodeValue;
    }
}

$realtitle = array_reduce(array($title2, $title1, $title3), function($a, $b) {
    return strlen($a) && $a != 'null' && strlen($a) < strlen($b) ? $a : $b;
}, null);

    echo 'metaogtitle: '.$title1 . '<br/><br/><br/><br/><br/>';
    echo 'name: '.$title2. '<br/><br/><br/><br/><br/>';
    echo 'name2: '.$title3. '<br/><br/><br/><br/><br/>';
    echo 'realtitle: '.$realtitle. '<br/><br/><br/><br/><br/>';
?>
  • 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-16T21:59:32+00:00Added an answer on June 16, 2026 at 9:59 pm
    // Filter invalid values
    $titles = array_filter($titles, function($title) { return $title && $title != 'null'; }); 
    // Just  sort :)
    usort ($titles, function ($left, $right) { return strlen($left) - strlen($right); });
    echo $titles[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

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.