View Full Version : need help w/ part of a dynamically generated title function

11-23-2009, 06:43 PM
I have this code:
$title = basename($_SERVER['SCRIPT_NAME'], '.php');
$title = ucfirst($title);
?> Basically it takes my page names, strips off the .php and capitalizes the first letter of the first word and puts it in the title.

My filenames are mostly like this.... signUp.php, videoView.php, videoUploadPrimer.php

I want to make my function find each capital letter and put a space before it to make the title... is there some variable for a 'capital letter' that i should use, how would this be done?

11-24-2009, 01:08 AM
you just want a space before the title? or if a page name is for example sign_up.php you want to echo as Sign Up? if the second
if the second you could look at erg replace and replace _ with a space ' '

11-24-2009, 01:23 AM
I need to put spaces between the words signUp...

like this: Sign Up

I am familiar with the replace ('_', ' ') All my filenames are camelCased.

I need a way to tell php to look for caps and put a space before them.:confused:

11-24-2009, 01:43 AM
should help you

11-24-2009, 02:07 AM
ok thanks, now i gotta try and figure out how to implement all that coder talk... still kinda new at all this. I'll get back on here when i solve it.8)

11-30-2009, 01:00 AM
Ok i need some help. I tried putting in some code from that link you gave edbr but i can seem to get any results.
$title = basename($_SERVER['SCRIPT_NAME'], '.php');
if ($title == 'index'){
$title = 'home';
$title = ucfirst($title);

I still am getting the geneated title with no spaces. tried echoing the $results varible but just get the word array... I have a feeling its something to do with that but can't figure it out.... thanks

12-10-2009, 02:56 AM
ok got it figured out, well kindof... i did it the $%&@!* rigged way:mrgreen:

in case anyone si curious it goes like this:
$title = basename($_SERVER['SCRIPT_NAME'], '.php');
if ($title == 'index'){
$title = 'Home';
$title = str_replace('A',' A',$title);
$title = str_replace('B',' B',$title);
$title = str_replace('C',' C',$title);
$title = str_replace('D',' D',$title);
$title = str_replace('E',' E',$title);
$title = str_replace('F',' F',$title);
$title = str_replace('G',' G',$title);
$title = str_replace('H',' H',$title);
$title = str_replace('I',' I',$title);
$title = str_replace('J',' J',$title);
$title = str_replace('K',' K',$title);
$title = str_replace('L',' L',$title);
$title = str_replace('M',' M',$title);
$title = str_replace('N',' N',$title);
$title = str_replace('O',' O',$title);
$title = str_replace('P',' P',$title);
$title = str_replace('Q',' Q',$title);
$title = str_replace('R',' R',$title);
$title = str_replace('S',' S',$title);
$title = str_replace('T',' T',$title);
$title = str_replace('U',' U',$title);
$title = str_replace('V',' V',$title);
$title = str_replace('W',' W',$title);
$title = str_replace('X',' X',$title);
$title = str_replace('Y',' Y',$title);
$title = str_replace('Z',' Z',$title);
$title = ucfirst($title);
?>That was an include file that was calleed to above the doctype on every page and then i had:
<?php echo "$title"; ?>That was in the title of each page and TADA...

My camelCased page names are now automatically generated as the title... i'm kinda proud of myself even though i did it the ghetto way... oh well whatever works

12-10-2009, 04:01 AM
good one , its a nice feeling right?

12-10-2009, 01:52 PM
Do i understand this correctly

$title = str_replace('A',' A',$title);

This looks like its finding an 'A' and replacing with an 'A'


12-10-2009, 04:30 PM
This looks like its finding an 'A' and replacing with an 'A'

No, its replacing an 'A' with ' A'. Notice the space before the A. That's what he wanted to achieve.

12-10-2009, 09:18 PM

couldnt see the wood for the trees

wouldnt this be better...

$title = " ".ucfirst($title);

rather than those str_replaces

12-10-2009, 09:44 PM

couldnt see the wood for the trees

wouldnt this be better...

$title = " ".ucfirst($title);rather than those str_replaces

correct me if im wrong but would that work for a camel cased name? ex: pictureUploadPrimer
I have ucfirst in my code already but it only works for the first letter of each word, and technically my names are all one word

12-11-2009, 07:00 AM
and you want to take this example -> pictureUploadPrimer

and turn it into -> Picture Upload Primer

you could use a regular expression and preg_replace

$title = ucfirst(preg_replace('/([A-Z])/',' \\1',$title));

12-11-2009, 04:52 PM
did my example help?

12-11-2009, 05:30 PM
why yes david, that does help... it works. Thank you, much more efficient than my way.:mrgreen:

Now one question.. what does all that mean??? i know what the uc first does, and can guess at what the rest is but im not sure... care to explain WHY that works how it does???

12-11-2009, 06:14 PM

we are using a preg_replace function

the reg in preg stands for regular expression or regex

regex is a language used to match patterns in strings. Most programming languages can utilise regex

lets look a little bit closer

the preg_replace function takes 3 arguments

preg_replace(arg1, arg2, arg3)

arg1 = needle (regex)
arg2 = replacement (regex)
arg3 = haystack (string)

Arg1 = /([A-Z])/

The two / either side are to define the expression start and finish

The ( brackets ) tell the expression engine to envoke group matching and allows storage of the matched values in the [ brackets ] for use later

The [A-Z] is what we are looking for. In this example we are looking for any uppercase letter from A - Z

Arg2 = \\1

This is whats known as a back reference. It tells the expression engine to replace with the value found in the match from the first argument within the brackets

You can use multiple backreferences

For example. Just count the brackets in the first arg and if you have used say 3


you can use backreferences like..

\\1 \\2 \\3

to perform a replace respectively. This is a bit like using a variable

also notice the space I included in the ' \\1' which adds the required space between words

Finally we have the $title variable which is just the haystack string

I am not 100% happy with it as we could have added a regex to upper case the first letter of the first word but overhead is negligible. Its just not perfect! Shame.

12-11-2009, 10:40 PM
That makes a lot more sense now... no use in using it if you dont know how it works, huh? I actually printed that off for future reference, Thanks so much for the lesson....:mrgreen: im gettin a lil better at this php stuff every day

12-11-2009, 10:45 PM
no problem

Always nice to help another dev out

thats how i started

12-12-2009, 12:16 AM
the guru, davidj is in the house :mrgreen: (because i know he just loves smileys)

12-12-2009, 09:01 AM
Im a different person now

I love smileys,I love smileys,I love smileys,I love smileys,I love smileys,I love smileys,I love smileys,I love smileys,I love smileys,I love smileys,I love smileys,I love smileys,I love smileys :confused: