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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:54:43+00:00 2026-05-24T12:54:43+00:00

I did this function in PHP to get a page’s title. I know it

  • 0

I did this function in PHP to get a page’s title. I know it might look a bit messy, but that’s because I’m a beginner in PHP. I have used preg_match("/<title>(.+)<\/title>/i",$returned_content,$m) inside the if before and it hasn’t worked as I expected.

function get_page_title($url) {
    $returned_content = get_url_contents($url);
    $returned_content = str_replace("\n", "", $returned_content);
    $returned_content = str_replace("\r", "", $returned_content);
    $lower_rc = strtolower($returned_content);
    $pos1 = strpos($lower_rc, "<title>") + strlen("<title>");
    $pos2 = strpos($lower_rc, "</title>");
    if ($pos2 > $pos1)
        return substr($returned_content, $pos1, $pos2-$pos1);
    else
        return $url;
}

This is what I get when I try to get the titles of the following pages using the function above:
http://www.google.com -> “302 Moved”
http://www.facebook.com -> “”http://www.facebook.com&#8221;
http://www.revistabula.com/posts/listas/100-links-para-clicar-antes-de-morrer -> “http://www.revistabula.com/posts/listas/100-links-para-clicar-antes-de-morrer&#8221;
(When I add a / to the end of the link, I can get the title successfully: “100 links para clicar antes de morrer | Revista Bula”)

My questions are:
– I know google is redirecting to my country’s mirror when i try to access google.com, but how can I get the title of the page it redirects to?
– What is wrong in my function that makes it get the title of some pages, but not of others?

  • 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-24T12:54:43+00:00Added an answer on May 24, 2026 at 12:54 pm

    HTTP clients should follow redirects. That 302 status code means that the content you tried to get isn’t at that location, and the client should follow the Location: header to figure out where it is.

    You have two problems here. The first is not following redirects. If you use cURL, you can get it to follow redirects by setting this:

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    

    See this question for a full solution:

    Make curl follow redirects?

    The second problem is that you are parsing HTML with RegEx. Don’t do that. See this question for better alternatives:

    How do you parse and process HTML/XML in PHP?

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

Sidebar

Related Questions

I did some googling to try to answer this question but even after that
I did this tests and the results seems the count function scale linearly. I
Working on this page: http://www.karlsenner.dreamhosters.com/about.php and having trouble with the navigation in IE6. It
hi have a php template that displays an html page. in it there is
I need help with changing a function that is called once the page loads,
I did this in the past, and can't remember the correct command (I think
i did this in msvc 2005. typedef void (*cleanup_t)(); void func(cleanup_t clean) { cleanup_t();
HI i did this code with help of Marc Gravell in Why can't I
I am stress testing a .NET web application. I did this for 2 reasons:
I am interested in writing utf-8 encoded strings to a file. I did this

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.