Welcome to WebmasterWorld Guest from 54.159.179.132

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Title Tag Problem

   
7:08 pm on Aug 29, 2008 (gmt 0)

5+ Year Member



Hi All:
I have the following code in a file to populate the title for each web page:

<?php
//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("II", "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);

?>

My issue is that I have a lot of webpages with the word "and" in them, and with the above code, it capitalizes each word, including "and". Is there a piece of code that I can put in the above file to segregate the word "and" to make it lowercase.

Thanks,

Michael

7:51 pm on Aug 29, 2008 (gmt 0)

5+ Year Member



I am not sure if this will work or not, its just what popped into my head, but couldn't you try a:


$newTitle = str_replace('AND', 'and', $newTitle);

After reforming the string?

9:16 pm on Aug 29, 2008 (gmt 0)

5+ Year Member



Well, I think I got it figured out, with you help, and adding one piece of code. It might be wrong, but it worked. Let me know if there is an easier way.

<?php
//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("and", "II", "php", "html"); //words to strtoupper() added "and" here to cap it

//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);
$newTitle = str_replace('AND', '&amp;', $newTitle); //added code you mentioned here, changing capped "AND" to the "&" symbol

?>

11:44 pm on Aug 29, 2008 (gmt 0)

5+ Year Member



I thought you had said it was always capped, but yes it is a good idea to insure that is capped if you are going to do the string replace with the ampersand. I'm glad to have been of assistance.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month