note 65712 added to function.ucwords

From: Date: Sat, 06 May 2006 08:44:09 +0000
Subject: note 65712 added to function.ucwords
Groups: php.notes 
Request: Send a blank email to php-notes+get-110683@lists.php.net to get a copy of this message
In the function ucsmart() posted by ieure at php dot net on 04-Dec-2005 11:57, I found a similar problem in this function to what he found in igua's. <?php function ucsmart($text) { return preg_replace('/([^a-z]|^)([a-z])/e', '"$1".strtoupper("$2")', strtolower($text)); } ?> "igua's code adds a backslash in front of the first single quote for me. This doesn't alter the content in any way other than changing case." Actually, it did end up changing the content for me (php 5.0.4) in the way that this function escapes a single quotation (apostrophe) in the MIDDLE of a word. For example: who's online? Became: Who\'s Online? The fix is simple however, and merely requires fine-tuning the regular expression: <?php function ucsmart($text) { return preg_replace('/([^a-z\']|^)([a-z])/e', '"$1".strtoupper("$2")', strtolower($text)); } ?> (note: while previewing this note before adding it, I am noticing php's website is not correctly displaying the change I made as I wrote it. After the first a-z in the expression, the single quotation should be escaped... If it isn't you will get a parse error! And apoligies if my text here is colored as php code; not my fault!) This will not escape a single quotation mark which occurs in the middle of a word... Though, you may find that might need to add other characters inside the regular expression if you use other special characters inside your words and if you get funky output. It's a great expression though! Simple, yet very powerful. Kudos! ---- Server IP: 129.16.214.50 Probable Submitter: 85.224.152.181 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.ucwords.php Edit -- http://master.php.net/note/edit/65712 Del: integrated -- http://master.php.net/note/delete/65712/integrated Del: useless -- http://master.php.net/note/delete/65712/useless Del: bad code -- http://master.php.net/note/delete/65712/bad+code Del: spam -- http://master.php.net/note/delete/65712/spam Del: non-english -- http://master.php.net/note/delete/65712/non-english Del: in docs -- http://master.php.net/note/delete/65712/in+docs Del: other reasons-- http://master.php.net/note/delete/65712 Reject -- http://master.php.net/note/reject/65712 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#110683) next »