SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert to small caps

    I've been trying to tackle this problem for a few hours now, so any help would be *very* much appreciated.

    Given a string like this: "Small Caps PHP Test", I want to turn it into:
    Code:
    S<small>MALL</small> C<small>APS</small> PHP T<small>EST</small>
    Which looks like: SMALL CAPS PHP TEST

    Instead of just <small>ing every letter other than the first in a word, I want to put <small> around every letter that's not an uppercase one.

    Any ideas? I know vBulletin does this for the titles of threads with polls, but that might be different.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this should do the trick.

    Code:
    $str = 'S<small>MALL</small> C<small>APS</small> PHP T<small>EST</small>';
    $str = preg_replace("/<small>([^>]*)<\/small>/e","strtolower('\\1')",$str);
    print $str;
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, but I think that's backwards. Doesn't that code take a string like "S<small>ITEPOINT</small>" and turn it into "Sitepoint"? I want to do the opposite.

    Here's what I have so far. It doesn't seem to work, though.

    PHP Code:
    #S returns an HTMLized small caps version of $str
    function small_caps($str) {
        
    $i 0;
        
    $last_letter_was_lowercase 0;
        while(
    $letter substr($str$i++, 1)) {
            if(
    is_upper($letter)) {
            
            
                if(
    $last_letter_was_lowercase) { 
                    
    $output .= $letter;
                } else {
                    
    $output .= "</small>$letter";
                }
                
    $last_letter_was_lowercase 0;
                
                
            } elseif(
    $letter != ' ') { 
            
            
                if(
    $last_letter_was_lowercase) {
                    
    $output .= '<small>'.strtoupper($letter);
                } else {
                    
    $output .= strtoupper($letter);
                }
                
                
            } else { 
    $output .= ' '; }
        }
        
        return 
    $output;
    }


    #S returns 1 if uppercase, 0 if not
    function is_upper($letter) {
        return (
    $letter == strtoupper($letter));


  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry Dude, but what on earth are you talking about? Do you mean change the font size? I mean what does the orginal string look like? And what does the intended string look like? How are they supposed to change?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    The original string will look like "I Love PHP And Diet Coke". I want to turn that in to "I L<small>OVE</small> PHP A<small>ND</small> D<small>IET</small> C<small>OKE</small>".

    So...
    for each letter in $str
    if the letter is uppercase, and the last letter was not lowercase, append the letter to $output
    if the letter is uppercase, and the last letter was lowercase, append </small> and the letter to $output
    if the letter is lowercase, and the last letter was lowercase, append the uppercase version of the letter to $output
    if the letter is lowercase, and the last letter was uppercase, append "<small>" and the uppercase version of the letter to $output

    Am I making any sense?

  6. #6
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I finally got it. Here's the final code:

    PHP Code:
    S returns an HTMLized small caps version of $str
    function small_caps($str) {
        
    $i 0;
        
    $output '';
        
    $last_letter_was_lowercase 0;
        while(
    $letter substr($str$i++, 1)) {
            if(
    is_upper($letter)) {
            
        
        
                        if(
    $last_letter_was_lowercase) { 
                            
    $output .= "</small>$letter";
                        } else {
                            
    $output .= $letter;
                        }
                        
    $last_letter_was_lowercase 0;
                
                
                
                
            } elseif(
    eregi("[^a-zA-Z]"$letter)) { 
                
    $output .= $letter;        
            } else {     
            
            
                        if(
    $last_letter_was_lowercase) {
                            
    $output .= strtoupper($letter);
                        } else {
                            
    $output .= '<small>'.strtoupper($letter);
                        }
                        
    $last_letter_was_lowercase 1;
            }
        }
        
        return 
    $output;
    }


    #S returns 1 if uppercase, 0 if not
    function is_upper($letter) {
        return (
    $letter == strtoupper($letter));

    Anyone understand what I wanted to do now? Thanks for trying though, I know I can be cryptic sometimes.

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, but here it is in a super swift three lines

    Code:
    $str = 'Small Caps PHP Test';
    $str = preg_replace("/([a-z]+)/e","strtoupper('<small>\\1</small>')",$str);
    print $str;
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm...

    ............
    ............
    ............

    /me bangs head on table


    Thank you freddie!


Bookmarks

Posting Permissions

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