1

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.

I say amending it as I use camel case and I'd need a way of turning userProfile.php into 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.

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'

In init.php I have

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'

I then get errors as it cannot locate the other includes within 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

I can then combine the two and voila

null
  • 3,469
  • 7
  • 41
  • 90
  • 1
    Yes, you can change the title. It's a very simple task. What are you stuck at? – user4035 Jul 22 '13 at 13:57
  • see http://stackoverflow.com/questions/4519739/split-camelcase-word-into-words-with-php-preg-match-regular-expression then implode and ucwords – Orangepill Jul 22 '13 at 13:58
  • Are your urls being rewritten or are you just serving an actual php file? – Pete Jul 22 '13 at 14:04

2 Answers2

1

To output the return value of your function in the title tags just put it this way :

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

in a php page.

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) .

Community
  • 1
  • 1
Jerska
  • 11,722
  • 4
  • 35
  • 54
  • Hi, I was literally just doing that! :) Although I now find I have another problem. Can I use a name for an include so no matter where the .php is in the folder structure it knows it's location? – null Jul 22 '13 at 14:12
0

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)));
Nathan
  • 1,700
  • 12
  • 15
  • hrm - the question has been edited significantly while I drafted this answer (= – Nathan Jul 22 '13 at 14:27
  • I ran out of questions and today is question day! :( Apologies, could you um, significantly change your answer to match said question? :) I tried the spl but receiving an error, still trying though – null Jul 22 '13 at 14:33