简体   繁体   English

页面的动态标题? PHP包括链接?

[英]Dynamic Title of page? PHP include links?

Can the Title of the page (within the head, not a random title) be dynamic? 页面标题(在标题内,不是随机标题)可以动态吗?

I was thinking that PHP could return the value of the current page and then echo it into the Title tag after amending it. 我以为PHP可以返回当前页面的值,然后在修改后将其回显到Title标签中。

I say amending it as I use camel case and I'd need a way of turning userProfile.php into User Profile 我说要修改它,因为我使用的是骆驼套,我需要一种将userProfile.php转换为User Profile的方法

Can anyone point me in the right direction please? 谁能指出我正确的方向?

I've used : 我用过:

public function findCurrentPageURL(){

return substr($_SERVER["SCRIPT_NAME"], strpos($_SERVER["SCRIPT_NAME"], "/")+1);

}//end to return current page

as a function and thought that the same principle could be applied. 作为功​​能,并认为可以应用相同的原理。

EDIT: I'm not sure how to word this but here goes, I'm hoping on a simple answer as I can't see this being a rare issue. 编辑:我不确定该如何措辞,但是在这里,我希望得到一个简单的答案,因为我看不到这是一个罕见的问题。

I have an init.php file that includes various classes and other files. 我有一个init.php文件,其中包括各种类和其他文件。

This gets called at the top of every page. 这在每个页面的顶部被调用。

It works fine when all the pages that call it are in the same folder. 当所有调用它的页面都在同一文件夹中时,它可以正常工作。 Now I need to create a subdirectory within the main so: 现在,我需要在主目录中创建一个子目录,以便:

coreFolder
->init.php
->classes
->->class.php
index.php
other.php
otherFolder
->otherPage.php

is an example of what I have now. 是我现在拥有的一个例子。

In index(and others) my call is require'core/init.php' 在索引(和其他)中,我的呼叫是require'core/init.php'

In init.php I have 在init.php中

require 'core/connect/dbConnect.php';
require 'core/classes/users.php';
etc etc

The problem I'm now having is when I try and call init from my otherPage.php I have to use include'../core/init.php' 我现在遇到的问题是当我尝试从我的otherPage.php调用init时必须使用include'../core/init.php'

I then get errors as it cannot locate the other includes within init.php. 然后我收到错误消息,因为它无法找到init.php中的其他包含项。

Is there a solution for this please? 请问有解决办法吗? I really would prefer to not have one uber great big long list of php files 我真的很想没有一个超级大而长的php文件列表

I can then combine the two and voila 然后我可以将两者结合起来

To output the return value of your function in the title tags just put it this way : 要在title标签中输出函数的返回值,只需将其放置为:

<title><?php echo findCurrentPageURL(); ?></title>

in a php page. 在php页面中。

Also to split it as you want, see Orangepill comment to the question, linking your question to Split camelCase word into words with php preg_match (Regular Expression) . 也可以根据需要拆分它,请参阅Orangepill对问题的评论,将您的问题与split camelCase单词链接为带有php preg_match(正则表达式)的单词

First, obtain the filename by: 首先,通过以下方式获取文件名:

  1. splitting the requested file path by '/' and obtaining the last item 用“ /”分割请求的文件路径并获得最后一项
  2. snipping off the file extension portion 剪掉文件扩展名部分

$filename = strstr(end(explode('/', $_SERVER["SCRIPT_NAME"])), '.', true);

Now that the filename is isolated: 现在文件名已被隔离:

  1. split into an array prior to upper-case letter 在大写字母之前拆分为数组
  2. translate the array into a string with spaces as delimiter 将数组转换为带有空格作为定界符的字符串
  3. make the first letter of initial word upper-case 将首字母大写的首字母大写

return ucfirst(join(' ', preg_split('/(?=[A-Z])/', $filename)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM