translate each word in string to uppercase with exceptions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    translate each word in string to uppercase with exceptions

    Hi,

    What i want to do is make the first letter of each word in a sting uppercase except "and" & "in"

    How would i go about doing this?

    Cheers,
    Adam
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    ucwords — Uppercase the first character of each word in a string

    $new_phrase = ucwords ( $phrase );

    Then you need to find all the and's and what ever else you don't want changed and changed them back:

    $final_phrase = str_replace ( ' And ' , ' and ' , $new_phrase );

    Haven't tried it, but you see my logic!

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      ~Here's something i quickly threw together, it's lengthy, and i'm sure someone can do it better, but here's my shot at it:
      [php]
      $_string = "going to make this uppercase but miss and & in"; # string to capitalise.
      $_string = explode(" ", $_string); # explode the string into an array
      $_ignore = array("and", "in"); # ignore list
      foreach($_strin g as $_key => $_val) # traverse the array
      {
      if(in_array($_v al, $_ignore)) # this $_val is in the ignore list
      {
      $_string[$_key] = $_val; # so
      }
      else # this $_val isn't in the ignore list
      {
      $_string[$_key] = ucfirst($_val); # so, using the $_key of the array, insert into relevant key.
      }
      }

      $_string = implode(" ", $_string); # Rebuild the string from remodeled array
      echo $_string;
      [/php]

      Hope it helps.
      Regards!

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Only problem is that, when the 'to be excluded word' is, in the text, followed by a reader sign like dot, comma, question or exclamation mark, etc, it is translated to upper case. SO it will need some adaptation to accomodate that.

        Ronald

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by ronverdonk
          Only problem is that, when the 'to be excluded word' is, in the text, followed by a reader sign like dot, comma, question or exclamation mark, etc, it is translated to upper case. SO it will need some adaptation to accomodate that.

          Ronald
          Ahhhh, touche!

          I shall have a look at what i can do to acomodate this challenge!

          Need to eat and watch a film first :)

          Regards.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Challenge during eating and movie watching ;-)

            Ronald

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Curious... What was wrong with my approach? I know it was simple, but I am here to learn!

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Originally posted by TheServant
                Curious... What was wrong with my approach? I know it was simple, but I am here to learn!
                It appears to me that your solution works best, also regarding the special reading marks. You did very well!

                Ronald

                Comment

                • adamjblakey
                  New Member
                  • Jan 2008
                  • 133

                  #9
                  Thanks for your replies everyone :)

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    We are here to help: you even have two answers to choose from.
                    See you netx time.

                    Ronald

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by TheServant
                      Curious... What was wrong with my approach? I know it was simple, but I am here to learn!
                      I hadn't even realised you posted before me!

                      Sorry.

                      Comment

                      • TheServant
                        Recognized Expert Top Contributor
                        • Feb 2008
                        • 1168

                        #12
                        Originally posted by ronverdonk
                        It appears to me that your solution works best, also regarding the special reading marks. You did very well!

                        Ronald
                        Thanks Ronald :D

                        Originally posted by markusn00b
                        I hadn't even realised you posted before me!

                        Sorry.
                        No problem ;)

                        Comment

                        Working...