Forum Moderators: coopster
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
$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]);
}
<?
//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>";
?>
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} –";?> 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} –";?> website.com</title>
and
<h1><?=$newTitle;?></h1>
Thanks for all the help.
Michael