SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: PHP Question

  1. #1
    Rusted & Weathered HumanClay's Avatar
    Join Date
    Sep 2000
    Location
    Milwaukee, WI
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Question

    I am querying a database for an Artist Name and storing it in $artist. I then use the following to obtain the first letter of the Name:

    PHP Code:
    $letter substr(ucfirst($artist),0,1); 
    I would like $letter to have the value of the first letter in the name capitalized (which it is right now with the above line of code). But if I have an Artist that begins with a non-alpha character (i.e. a number or a symbol), I want $letter to have the value of "Other" instead of the letter of the Artist. How do I achieve this?
    Steve Caponetto - [profile] [e-mail]
    CreedFeed.com - feed your need for Creed!

  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)
    PHP Code:
    $artist "Barry Manilow";
    $letter = (ereg("^[a-zA-Z]{1}"$artist)) ? ucfirst(substr($artist,0,1)) : "Other";

    //Prints B
    print $letter;


    $artist "!Barry Manilow";
    $letter = (ereg("^[a-zA-Z]{1}"$artist)) ? ucfirst(substr($artist,0,1)) : "Other";

    //Prints Other
    print $letter
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Rusted & Weathered HumanClay's Avatar
    Join Date
    Sep 2000
    Location
    Milwaukee, WI
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot freddy!
    Steve Caponetto - [profile] [e-mail]
    CreedFeed.com - feed your need for Creed!

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HumanClay, unfortunately, freddydoesphp's solution solution is way too overkill and quite inefficient for what you are after, the only reason I say this (no offense freddydoesphp), is because by doing things this way you are well on your way to a poor coding style. There are string functions built in to PHP to cover just about every imaginable situation you may come across.

    Use this code instead:
    PHP Code:
    <?php
    $first 
    strtoupper((substr($artist01)));
    $ascii_val=ord($first);

    //ascii codes: 'A' = 65, 'Z' = 90
    if ($ascii_val >= 65 && $ascii_val <= 90)
    {
        
    $letter=$first;
    }
    else
    {
        
    $letter='Other';
    }
    Last edited by 7stud; Jun 12, 2001 at 18:46.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whatever
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just because I was interested, I ran benchmark tests on both of our methods, here are the results.

    Yours
    Code:
    Server Software:        Apache/1.3.12                                      
    Server Port:            80
    
    Document Path:          /~freddy/ord.php
    Document Length:        1 bytes
    
    Concurrency Level:      5
    Time taken for tests:   2.850 seconds
    Complete requests:      1000
    Failed requests:        0
    Total transferred:      248248 bytes
    HTML transferred:       1001 bytes
    Requests per second:    350.88
    Transfer rate:          87.10 kb/s received
    
    Connnection Times (ms)
                  min   avg   max
    Connect:        0     3    61
    Processing:     2    10    11
    Total:          2    13    72

    Mine
    Code:
    Server Software:        Apache/1.3.12                                      
    Server Port:            80
    
    Document Path:          /~freddy/mine.php
    Document Length:        1 bytes
    
    Concurrency Level:      5
    Time taken for tests:   1.648 seconds
    Complete requests:      1000
    Failed requests:        0
    Total transferred:      248248 bytes
    HTML transferred:       1001 bytes
    Requests per second:    606.80
    Transfer rate:          150.64 kb/s received
    
    Connnection Times (ms)
                  min   avg   max
    Connect:        0     1     7
    Processing:     2     6    39
    Total:          2     7    46
    Last edited by freddydoesphp; Jun 12, 2001 at 19:44.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Los Angeles, California
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey freddy, how did you run those tests?
    www.iyeinteractive.com - Website Design and Programming
    www.totalnonsense.net - my site, Under Construction

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the apache/bin folder there is a program named ab. Do a ab --usage or ab --help to get instructions. Normally its

    $>ab -n numberofrequests -c numberofconcurrentusers http://www.somedomain.com/sometest.php
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Burn!


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
  •