Page 1 of 1
simple lcwords
Posted: Mon Dec 11, 2006 12:49 am
by s.dot
The opposite of ucwords. Don't know why you'd need it, but I'm playing around with making a text transformer with a bunch of options.
the function
Code: Select all
function lcwords($text)
{
$text = explode(' ', $text);
$new_string = array();
foreach($text AS $word)
{
$strlen = strlen($word);
for($i=0; $i<$strlen; $i++)
{
if(preg_match("/\w/", $word[$i]))
{
$word[$i] = strtolower($word[$i]);
break;
}
}
$new_string[] = $word;
}
$text = implode(' ', $new_string);
return $text;
}
test
Code: Select all
$string = 'The Colts will make the playoffs, but the ...Jaguars continued to open the wound that is
their rush defense. Vic Carucci wonders how far Indy can go in his snapshots.';
echo lcwords(strtoupper($string));
result
Code: Select all
tHE cOLTS wILL mAKE tHE pLAYOFFS, bUT tHE ...jAGUARS cONTINUED tO oPEN tHE wOUND tHAT iS tHEIR rUSH dEFENSE. vIC cARUCCI wONDERS hOW fAR iNDY cAN gO iN hIS sNAPSHOTS.
Posted: Mon Dec 11, 2006 7:19 pm
by Ambush Commander
It seems to be very inefficient to me: every word gets a preg_match called on it.
Posted: Mon Dec 11, 2006 7:27 pm
by John Cartwright
Ambush Commander wrote:It seems to be very inefficient to me: every word gets a preg_match called on it.
I was thinking that myself, this could pretty easily be done with preg_replace() me thinks..
Posted: Mon Dec 11, 2006 7:29 pm
by feyd
If the intent is in fact the opposite of
ucwords(), a simple transform would probably have superior performance.
Posted: Tue Dec 12, 2006 3:46 pm
by Ollie Saunders
Code: Select all
function lcwords($str)
{
static $function;
if (!$function) {
$function = create_function('$str', 'return strtolower($str[0]);');
}
return preg_replace_callback('~\b\w~', $function, $str);
}
I just did a test and the use of static gives a 28% performance boost.
Posted: Tue Dec 12, 2006 11:39 pm
by s.dot
Nice.
Posted: Thu Dec 14, 2006 5:47 am
by Mordred
ole wrote:Code: Select all
function lcwords($str)
{
static $function;
if (!$function) {
$function = create_function('$str', 'return strtolower($str[0]);');
}
return preg_replace_callback('~\b\w~', $function, $str);
}
I just did a test and the use of static gives a 28% performance boost.
Why use create_function() at all?
Posted: Thu Dec 14, 2006 5:58 am
by Ollie Saunders
To avoid a one line function littering the namespace.
Posted: Thu Dec 14, 2006 6:09 am
by Mordred
Are you aware that in this manner you practically lose the said 28% performance boost? Plus the yet unmeasured additional boost of pre-compiled code, which create_function() can't use.
Come on! "Littering the namespace" is worse than fast(er) code?
Posted: Thu Dec 14, 2006 6:28 am
by Ollie Saunders
I'm surprised you feel so strongly. Its really a matter context. Which is more important: namespace preservation or performance?
I just happened to choose namespace preservation because all I'm doing at the moment, in terms of code, is writing a library. A good library doesn't litter the namespace.
Whilst I choose namespace preservation over performance I didn't neglate performance entirely by using a static that circumvents most of the performance loss by using create_function. I only did that because it was easy to do and I knew it would work. Having said all that with a class there isn't a problem:
Code: Select all
class StringProcesses
{
public function lcwords($str)
{
return preg_replace_callback('~\b\w~', array($this, '_lcwordsCallback'), $str);
}
private function _lcwordsCallback($matches)
{
return strtolower($matches[0]);
}
}
Come on! "Littering the namespace" is worse than fast(er) code?
How much faster?
Posted: Thu Dec 14, 2006 6:41 am
by Mordred
ole wrote:Having said all that with a class there isn't a problem
Ah, this is indeed a perfect solution for the objections of both of us!
ole wrote:How much faster?
28%
If you test it with a loop indeed the function will be created only once, which is perfect. BUT, If you have a script that calls this just a couple of times, but is hit many times by different users, that busyness with the "static" function will not be of much use, hence you'll have the same performance loss as when not using static. What I was so passionately against is making wrong decisions based on incorrectly made tests.
Posted: Thu Dec 14, 2006 6:58 am
by Ollie Saunders
Contextual again...If the lcwords using the static is only being executed once and you are having performance issues from it
(in my opinion this is an unlikely scenario but I'll play along anyway) then yes using an external function is a good idea, if its being executed
more than once then..
...is a good idea as it has a really tiny overhead.
In any case an external function will, yes, always be faster. But until I have performance issues I'm going to preserve my namespace
Some clever bloke wrote:Premature optimization is the root of all evil