bulrush Posted August 18, 2010 Share Posted August 18, 2010 I thought this was pretty straight forward, but it is not working. Titlecase is supposed to capitalize the first letter of every with except for certain words in $smallarr. Instead, it capitalizes every word. What's wrong here? //==================================================== function crTitlecase($s) /* Capitalize every word in a sentence except certain small words. */ { //Keep $smallarr in alpha order! //Do not capitalize these words. $smallarr=array('a','above','and','an','at','by','for','if'); $smallarr=array($smallarr,'into','is','it','of','off'); $smallarr=array($smallarr,'on','or','over','the','to'); $smallarr=array($smallarr,'with','without'); //$smallarr=($smallarr,); $t=strtolower($s); //First change all ltrs to lowercase. $words = explode(' ', $t); foreach ($words as $mykey => $word) { //if ((!$mykey) or (!in_array($word, $smallarr)) ) if ( (!in_array($word, $smallarr)) ) { $s='mykey='.$mykey.', word='.$words[$mykey]; crDebug($s); $words[$mykey] = ucwords($word); } } $newtitle = implode(' ', $words); return $newtitle; } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 18, 2010 Share Posted August 18, 2010 I haven't looked through it all, but first off, you're building a four dimensional array that won't work as needed in the in_array(). Try replacing your multiple array definitions with: $smallarr = array('a','above','and','an','at','by','for','if','into','is','it','of','off','on','or','over','the','to','with','without'); Quote Link to comment Share on other sites More sharing options...
bulrush Posted August 18, 2010 Author Share Posted August 18, 2010 That might be my problem. How would I divide this among multiple lines for readability? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 18, 2010 Share Posted August 18, 2010 Many ways. Here's one: I haven't looked through it all, but first off, you're building a four dimensional array that won't work as needed in the in_array(). Try replacing your multiple array definitions with: $smallarr = array( 'a','above','and','an','at','by', 'for','if','into','is','it','of', 'off','on','or','over','the','to', 'with','without' ); Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 18, 2010 Share Posted August 18, 2010 Just for fun, this will do it and also make sure the first word in the string is capitalized: function crTitlecase($s) { $smallarr = array( 'a','above','and','an','at','by', 'for','if','into','is','it','of', 'off','on','or','over','the','to', 'with','without' ); foreach(explode(' ', ucfirst(strtolower($s))) as $word) { if(!in_array($word, $smallarr)) { $words[] = ucfirst($word); } else { $words[] = $word; } } return implode(' ', $words); } Quote Link to comment Share on other sites More sharing options...
bulrush Posted August 18, 2010 Author Share Posted August 18, 2010 Thanks! I'll try it. ... It worked! I couldn't find that syntax in any manual. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.