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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:41:04+00:00 2026-05-28T04:41:04+00:00

Which php array function should be used to match a string to the first

  • 0

Which php array function should be used to match a string to the first part of a delimited line and return the second part of the delimited line? The reason I am using an array is because I have many delimited text lines in a file. ex:

contact-us.php = Contact Us- Test Bed

I need some way to match the pages filename to the first part of the delimited line, and return it’s second part. I have tried a few different array functions but I do not know which one to use or how to implement the array function assuming that I have found the correct one. This is the code that I devised and it is located in the head of the php file. Once the correct page title is chosen, I will simply print it into the title tags.

function getPageName()
{
    return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1); // If one echo's this and the url is /TestBed/contact-us.php Output will be: contact-us.php
}

function pageTitleIdentifier ()
{
    $filename = 'http://localhost/TestBed/includes/apptop/title.txt';
    $mode = 'rb';
    $file_handle = fopen ($filename, $mode);

    while (!feof($file_handle) ) {
        $page_title_pair = fgets($file_handle); // This will start reading where the above while loop stopped line by line.
        $parts = explode('=', $page_title_pair);
        @ $pageTitle = $parts[0] . $parts[1]; // Part zero is the filename ex contact-us.php Part one is the Title ex Contact Us- Test Bed for that page.
    }

    fclose($file_handle);
}

So, what is the correct way to do this? Thank you very much!

  • 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-28T04:41:05+00:00Added an answer on May 28, 2026 at 4:41 am

    First off, you may want to consider implementing a caching solution. To parse a file for EVERY request, on a high-traffic server, will definitely add un-necessary load.

    Try this:

    function pageTitleIdentifier ()
    {
        $filename = 'http://localhost/TestBed/includes/apptop/title.txt';
        $mode = 'rb';
        $file_handle = fopen ($filename, $mode);
    
        while (!feof($file_handle)) {
    
            $page_title_pair = fgets($file_handle); 
    
            list($script, $title) = explode('=', $page_title_pair);
    
            // Uncomment below lines for debugging, they must line up exactly
            //   for the condition to be met, breaking the loop
            // var_dump($script);
            // var_dump(getPageName();
    
            // Because we reading from a file, might be some whitespace issues
            //  trim and strtolower ensures a true apples to apples comparison
            if (trim(strtolower($script)) === trim(strtolower(getPageName()))) {
                return $title;
            }            
        }
    
        fclose($file_handle);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function language($tag) that requires a file lang.php which contains an array
I've a web-service function which is returning an array of items to a PHP-Client.
How to implement a php Function which accepts a single array of integers and
I have the following Javascript function that should return an array of groups that
PHP functions such as 'array_map' take a callback, which can be a simple function
I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1]
I have an array in PHP which holds a bunch of unix timestamps. As
i have one php file which process adding of record in Database fro array.
A php script is giving this array (which has been passed through json_encode() )
Which is better to use in PHP, a 2D array or a class? I've

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.