homepage Welcome to WebmasterWorld Guest from 54.211.181.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Title Tag Problem
mkingsle

5+ Year Member



 
Msg#: 3734578 posted 7:08 pm on Aug 29, 2008 (gmt 0)

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

 

shdwmage

5+ Year Member



 
Msg#: 3734578 posted 7:51 pm on Aug 29, 2008 (gmt 0)

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?

mkingsle

5+ Year Member



 
Msg#: 3734578 posted 9:16 pm on Aug 29, 2008 (gmt 0)

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

?>

shdwmage

5+ Year Member



 
Msg#: 3734578 posted 11:44 pm on Aug 29, 2008 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved