views:

182

answers:

1

Hi,

I'm writing bug tracking software in PHP, and today I saw this in another bug tracker: http://bugs.php.net/bug.php?id=12017

Now I want to add a feature in my software which will block titles where at least 75% of all characters is uppercase.

How can I do this? Thanks,

P.S. CSS will not work, because, for example if you have the word 'iPod' in your title it turns into 'ipod' which sucks :)

+11  A: 
strlen(preg_replace('/[A-Z]/', '', $str)) / strlen($str) > 0.25

Not tested, but you should get the idea.

Ignas R
I'm thinking integer division problems here. Perhaps switching them? (original_length / changed_length) > 4?
Simon Svensson
Awesome it works!
Time Machine
do a check for (strlen($str) <> 0)...
egon
Well, I presume that $str is validated for being non-empty beforehand. After all, it's an user-submitted title.
Ignas R
And the division won't cause any problems. Even if both operands of the `/` operator are integers, the result will be a float unless they divide evenly.
Ignas R
If you are (possibly) dealing with letters other than a-zA-Z like e.g. a danish Æ or a spanish Ñ you should take a look at the unicode support of pcre. The property \p{Lu} tests for an uper-case character. see http://www.pcre.org/pcre.txt ("UTF-8 AND UNICODE PROPERTY SUPPORT")
VolkerK
Do you want I give credit in the source code?
Time Machine
@Koning Baard XIV, no, of course no, thanks. It's just a small and obvious snippet of code...
Ignas R