Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Caps Fix

  1. #1
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts

    Caps Fix

    Trying to write a function to fix capitalization in an input field to only allow 1 cap per word, and a max of 4 caps per sentence. I think I'm on the right track, but it's returning a buttload of errors.

    PHP Code:
    function caps_check($string) {
     
    $sentences split("."$string);
     
    $newsentences = array();
     
    $newwords = array();

     foreach (
    $sentences as $sentence) {
      
    $words split(" "$sentences);
       foreach (
    $words as $word) {
        
    $letters split($words);
         foreach (
    $letters as $letter) {
          if (
    $letter strtoupper($letter)) {
           
    $j $j 1;
          }
         }
          if (
    $j 1) {
           
    $word strtolower($word);
          }
        
    array_push($newwords$word);
       }
       if (
    $j 4) {
        
    $sentence strtolower($sentence);
       }
      
    array_push($newsentences$sentence);
     }
      return 
    join(""$newsentences);

    Last edited by Deacon Frost; 01-07-2010 at 01:03 PM.

  • #2
    Regular Coder Stooshie's Avatar
    Join Date
    Mar 2008
    Location
    Dundee, Scotland
    Posts
    380
    Thanks
    9
    Thanked 39 Times in 39 Posts
    Firstly, split() is now deprecated. use explode instead (it's faster anyway).

    What are the errors you are getting?
    Regards, Stooshie
    O

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    On a quick look, the code
    Code:
    if ($letter = strtoupper($letter))
    is wrong! It should be ==
    .. and you'd need to reset the value of $j before your loops.
    Last edited by abduraooft; 01-07-2010 at 11:50 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by abduraooft View Post
    On a quick look, the code
    Code:
    if ($letter = strtoupper($letter))
    is wrong! It should be ==
    .. and you'd need to reset the value of $j before your loops.
    Haha ^.^ Thanks guys. The errors I was returning was for the split() and one of the foreach(). I've completely revamped it, but I'm still working on the limiting the number of caps in a sentence.

    PHP Code:
    function caps_check($string) {
     
    $newwords = array();
      
    $words explode(" "$string);

       foreach (
    $words as $word) {
        
    $letters str_split($word);
         foreach (
    $letters as $letter) {
          if (
    $letter == strtoupper($letter)) {
           
    $j $j 1;
          }
         }
          if (
    $j >= 4) {
           
    $word strtolower($word);
          }
           
    array_push($newwords$word);
            unset(
    $j);
       }
      foreach (
    $newwords as $value) {
       
    $str $str." ".$value;
      }
       return 
    $str;


  • #5
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    Now it just returns 'Array' >.< :P.

    PHP Code:
    function caps_check($string) {
     
    $sentences explode("."$string);
     
    $newsentences = array();

     foreach (
    $sentences as $sentence) {
      
    $newwords = array();
      
    $words explode(" "$sentence);
       foreach (
    $words as $word) {
        
    $letters str_split($word);
         foreach (
    $letters as $letter) {
           if (
    $letter == strtoupper($letter)) {
            
    $j $j 1;
           }
         }
          if (
    $j >= 4) {
           
    $word strtolower($word);
           
    $word ucfirst($word);
          }
           
    $word $word." ";
         
    array_push($newwords$word);
       }
        if (
    $j >= 20) {
         
    $sentence strtolower($sentence);
         
    $sentence ucfirst($sentence);
        }
        
    array_push($newsentences$newwords);
       
    $j 0;
     }
      foreach (
    $newsentences as $val) {
       if (
    $str) {
        
    $str $str.". ".$val;
       }
        else {
         
    $str $val;
        }
      }
       return 
    $str;

    If I put in "Test. Test."

    It returns "Array. Array. Array" >.<. Lol.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •