应使用哪个 PHP 数组函数将字符串与分隔行的第一部分匹配并返回分隔行的第二部分
应该使用哪个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您可能需要考虑实施缓存解决方案。在高流量服务器上为每个请求解析文件肯定会增加不必要的负载。
试试这个:
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: