SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fomating text from the database

    hi all

    just a quick question!

    just manged to show my data from the database, but all the venues and reviews are in lowercase.

    how can i have the first letter become Capatil, no matter what way its been added to the database?

    many thanks

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $text = ucwords($text);

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    were would i fit that in to the script

    i want the first charcter of the [venue] and [comment] to be CAP, these are my fields in the database

    #Fetch and print all the records.

    while ($row = mysql_fetch_array ($result)) {
    echo '<div id review>' . $row['venue'] . '<br />' . $row['review_date'] . '<p />'
    . $row['comment'] . '<p /> </div>';

    }

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just change
    $row['venue']

    to

    ucfirst(ltrim($row['venue']))

    same for comment

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    many thanks crusher!!!

  6. #6
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats great and works , but what if i have 2 words? how can i get it to affect both?

    thanks!

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the first 2 words in a string?

    or are you asking how to make $row['comment'] work too?

  8. #8
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, the first 2 words in a string.

    and is it possible to hav it after every full stop aswell?

  9. #9
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any 1?

  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use a regular expression for that.
    Here is some code that should do the trick. I have checked it so it should work as is...

    PHP Code:
    $text '
    first line. this shOuLd wORK.
    new LINE. this is. a tEsT
    '
    ;

    $text preg_replace_callback('/(^|\.\s+)(\w+)(\s*\w*)/''my_ucfirst'trim(strtolower($text)));

    echo 
    $text;

    Function 
    my_ucfirst($arrMatches){
        
    $string '';
        
        unset(
    $arrMatches[0]); // get rid of the first match as it contains the whole term
        
        
    foreach ($arrMatches as $word){
            
    $string .= ucfirst($word);
        }
        
        
        return 
    trim($string);

    will return

    PHP Code:
    First lineThis should work.
    New 
    lineThis isA test 
    www.SQLrecipes.com A free cookbook for SQL recipes.

    I didn't believe someone could make over $19,000 a month...
    ...with Google Adsense, until I read this.

  11. #11
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks 4 the input but

    ive just changed

    $row['venue']

    to

    ucfirst(ltrim($row['venue']))

    this changed the first letter to a cap

    do u really need all that code to achive this?

    if so how would i add it tp

    $row['comment']

  12. #12
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why dont you just put proper sentences into the database in the first place?

  13. #13
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    because i wont be writing them, i will be the people who r surfing my site.

    thanks

  14. #14
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no really quick and easy solution.

    Place these two functions in your script somewhere:

    PHP Code:
    Function my_ucfirst($text){
        return 
    preg_replace_callback('/(^|\.\s+)(\w+)(\s*\w*)/''my_ucfirst_callback'trim(strtolower($text)));
    }

    Function 
    my_ucfirst_callback($arrMatches){
        
    $string '';
        
        unset(
    $arrMatches[0]); // get rid of the first match as it contains the whole term
        
        
    foreach ($arrMatches as $word){
            
    $string .= ucfirst($word);
        }
        
        
        return 
    trim($string);

    and then all you have to do is:

    PHP Code:
    while ($row mysql_fetch_array ($result)) {
        echo 
    '<div id review>' my_ucfirst($row['venue']) . '<br />' $row['review_date'] . '<p />' my_ucfirst($row['comment']) . '<p /> </div>';

    www.SQLrecipes.com A free cookbook for SQL recipes.

    I didn't believe someone could make over $19,000 a month...
    ...with Google Adsense, until I read this.

  15. #15
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks alot wwms!!

    it works but, for some reason my_ucfirst dosent turn blue like all the other functions in my wysiwug

    thanks every1

  16. #16
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwms
    There is no really quick and easy solution.

    Place these two functions in your script somewhere:

    PHP Code:
    Function my_ucfirst($text){
        return 
    preg_replace_callback('/(^|\.\s+)(\w+)(\s*\w*)/''my_ucfirst_callback'trim(strtolower($text)));
    }

    Function 
    my_ucfirst_callback($arrMatches){
        
    $string '';
        
        unset(
    $arrMatches[0]); // get rid of the first match as it contains the whole term
        
        
    foreach ($arrMatches as $word){
            
    $string .= ucfirst($word);
        }
        
        
        return 
    trim($string);

    and then all you have to do is:

    PHP Code:
    while ($row mysql_fetch_array ($result)) {
        echo 
    '<div id review>' my_ucfirst($row['venue']) . '<br />' $row['review_date'] . '<p />' my_ucfirst($row['comment']) . '<p /> </div>';

    is there any way u could put some comments to this script please, just so i no what it all means, thanks again


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
  •