Forum Moderators: coopster

Message Too Old, No Replies

Title Tag Problem

         

mkingsle

3:37 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



Hi everyone:

I am having a little difficulty and could really use some help. Seems pretty easy, but i am getting hung up somewhere.

I am using the following code in an include file in order to strip out the file name and then use that in the title meta tag:

<?php
$title = basename($_SERVER['SCRIPT_NAME'], '.php');
$title = str_replace('_', ' ', $title);
$title = ucwords($title);
?>

This works excellent. But I have one problem. One of my pages has the page name css_website_design.php

As you can see by the title, when using the ucwords command, it's going to give a title of "Css Website Design", only capitalizing the first letter of each word.

What I would like is to capitalize each letter of the first word..."CSS" and then have it continue to capitalize just the first letter of each word thereafter.

I have looked at the strtoupper command, but that puts all words in uppercase. I could use that, but it would be a compromise.

Thanks in advance for any help.

Michael

d40sithui

4:09 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



1) you can use the substr() function

$str = "css web design";
$newStr = strtoupper(substr($str, 0, 3)).ucwords(substr($str, 4, strlen($str)));

2) although it might be easier if you use arrays. this way you can modify each word and its more flexible if you have other strings you want converted the same way.

$str = "css web design";
$strArr = explode(" ", $str);
$newStr = strtoupper($strArr[0]);
for($i = 1; $i < sizeof($strArr); $i++){
$newStr.=" ".ucwords($strArr[$i]);
}

mkingsle

4:18 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



I appreciate your reply. I am still a little bit confused, as I am a newby to php.

Do I add this code to my current function. Do i need to change anything?

Thanks

d40sithui

5:20 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



change that part of your script to this


<?
//this part remains the same
$title = basename($_SERVER['SCRIPT_NAME'], '.php');
$title = str_replace('_', ' ', $title);

//below are changes
$title = strtolower($title);
$titleArr = explode(" ", $title); //creating an array of words from $title
$strToCaps= array("css", "php", "html"); //words to strtoupper()

//running strtoupper() with all words found in $strToCaps and ucwords() for all others
foreach(array_keys($titleArr) as $key){
if(in_array($titleArr[$key], $strToCaps)){
$titleArr[$key] = strtoupper($titleArr[$key]);
}
else{
$titleArr[$key] = ucwords($titleArr[$key]);
}
}

//reforming the string
$newTitle = implode(" ", $titleArr);
echo "\$newTitle: $newTitle<br>";

?>

adwatson

5:21 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



I'd say just add a line after "$title = str_replace..." to check for "Css" and capitalize it...

Something like this (untested)
$title = ereg_replace("Css","CSS",$title);

Which will replace "Css" with "CSS" everywhere in the title.

whoisgregg

5:35 pm on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For a simple search like that one, you can just use str_replace. It'll be a bit faster. And, even for complex searches, preg_replace is often faster (according to the manual [php.net]).

mkingsle

6:09 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



Got it working!

Thanks for the help.

I made one change:

//reforming the string
$newTitle = implode(" ", $titleArr);
echo "\$newTitle: $newTitle<br>";

?>

Hopefully it is correct for me to take out the echo command. I didn't want it printing on the page at the top. I have used it in:

<h1><?=$newTitle;?></h1>

So now it populates in my title tag as well:

<title><?php echo "{$newTitle}&nbsp;&ndash;";?> website.com</title>

Hopefully I'm right, it works, but I'm always unsure if everything is kosher.

So to summarize, the final code I used in the php script was:

<?php

$title = basename($_SERVER['SCRIPT_NAME'], '.php');
$title = str_replace('_', ' ', $title);
$title = strtolower($title);
$titleArr = explode(" ", $title);
$strToCaps= array("css", "php", "html");
foreach(array_keys($titleArr) as $key){
if(in_array($titleArr[$key], $strToCaps)){
$titleArr[$key] = strtoupper($titleArr[$key]);
}
else{
$titleArr[$key] = ucwords($titleArr[$key]);
}
}
$newTitle = implode(" ", $titleArr);

?>

And on each individual page I used:

<title><?php echo "{$newTitle}&nbsp;&ndash;";?> website.com</title>

and

<h1><?=$newTitle;?></h1>

Thanks for all the help.

Michael