/    Sign up×
Community /Pin to ProfileBookmark

[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 [U]aoga7sha3a0a[/U] or something, then the 7, 3, and 0 should be [COLOR=”Red”]red[/COLOR]. I’m thinking a simple…

[code=html]<span class=”rednum”>7</span>[/code]

…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!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 25.2012 — [code=php]
preg_replace('/[0-9]/', '<span class="rednum">$0</span>', $string)
[/code]
Copy linkTweet thisAlerts:
@amandaNHTauthorApr 25.2012 — Thanks! ?
Copy linkTweet thisAlerts:
@NogDogApr 25.2012 — [code=php]
preg_replace('/[0-9]/', '<span class="rednum">$0</span>', $string)
[/code]
[/QUOTE]


You could clean up the HTML a bit with the following small change, so that consecutive number characters would be in one span:
[code=php]
preg_replace('/[0-9]+/', '<span class="rednum">$0</span>', $string)
[/code]
×

Success!

Help @amandaNHT spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...