www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] PHP Function to check if a character in a string is a number vs a letter

  1. #1
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265

    resolved [RESOLVED] PHP Function to check if a character in a string is a number vs a letter

    I have a string of a random length and I need all of the NUMBERS in the string to display as red... like if the string is aoga7sha3a0a or something, then the 7, 3, and 0 should be red. I'm thinking a simple...
    HTML Code:
    <span class="rednum">7</span>
    ...should work just fine, but my problem is getting them there... I need a PHP function that can check each character in the string and if it's a number (which would only be 0 1 2 3 4 5 6 7 8 or 9) then it adds the "stuff" around it to make it red...

    The reason I'm doing this is so that when the administrator looks at the list of usernames and passwords she doesn't tell them (on the phone if they're having trouble logging in) to put in 101 if it's loi or something...

    Any suggestions would be greatly appreciated. Thanks!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,432
    PHP Code:
    preg_replace('/[0-9]/''<span class="rednum">$0</span>'$string
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    Thanks!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,432
    Quote Originally Posted by NogDog View Post
    PHP Code:
    preg_replace('/[0-9]/''<span class="rednum">$0</span>'$string
    You could clean up the HTML a bit with the following small change, so that consecutive number characters would be in one span:
    PHP Code:
    preg_replace('/[0-9]+/''<span class="rednum">$0</span>'$string
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles