【问题标题】: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应使用哪个 PHP 数组函数将字符串与分隔线的第一部分匹配并返回分隔线的第二部分
【发布时间】:2012-02-11 20:16:48
【问题描述】:

应该使用哪个 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);
}

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

【问题讨论】:

    标签: php arrays explode csv


    【解决方案1】:

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

    试试这个:

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

    【讨论】:

    • 嗯。它不工作。它没有给出任何错误,即使它设置为“通知”。我将这一行更改为 print_r (list($page, $title) = explode('=', $page_title_pair)); 它打印: Array ( [0] => index.php [1] => Test Bed ) Array ( [0] => news.php [1] => News- Test Bed ) Array ( [0] => contact-us.php [1] => Contact Us- Test Bed ) Array ( [0] => about.php [1] => About- Test Bed ) 每一行都是一个新的数组,但我认为在读取所有文件后会检查 $script,就像 $title 一样。我将如何更改它以便只读取一行然后进行比较?
    • 执行 var_dump(getPageName()),然后发布结果。
    • 显然情况并非如此,因为这样修改代码:print_r (list($page, $title) = explode('=', $page_title_pair)); // $script is the filename ex contact-us.php $title is the Title for that page. print 'Next'; 打印:Array ([0] => index.php [1] => Test Bed) NextArray ([0 ] => news.php [1] => News- Test Bed ) NextArray ( [0] => contact-us.php [1] => Contact Us- Test Bed ) NextArray ( [0] => about.php [ 1] => About-Test Bed) Next
    • 我会尝试一下......现在不能。你实际上回复了,我没有注意到,所以我上面的评论并不意味着你所说的不起作用,因为我的评论在某种程度上应该高于你的。
    • 注意:文本文件中目前有 4 行。好的,当 var_dump[...] 在 if 循环中时,什么都不会发生,这意味着 if 循环永远不会为真。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多