应使用哪个 PHP 数组函数将字符串与分隔行的第一部分匹配并返回分隔行的第二部分

发布于 2024-12-27 03:38:13 字数 1113 浏览 1 评论 0原文

应该使用哪个 php 数组函数将字符串与分隔行的第一部分匹配并返回分隔行的第二部分?我使用数组的原因是因为文件中有许多分隔文本行。例如:

contact-us.php = Contact Us- Test Bed

我需要某种方法将页面文件名与分隔行的第一部分匹配,并返回它的第二部分。我尝试了几种不同的数组函数,但我不知道该使用哪一种,也不知道如何实现该数组函数(假设我找到了正确的数组函数)。这是我设计的代码,它位于 php 文件的头部。一旦选择了正确的页面标题,我只需将其打印到标题标签中即可。

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);
}

那么,这样做的正确方法是什么?非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2025-01-03 03:38:13

首先,您可能需要考虑实施缓存解决方案。在高流量服务器上为每个请求解析文件肯定会增加不必要的负载。

试试这个:

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文