simple lcwords

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

simple lcwords

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It seems to be very inefficient to me: every word gets a preg_match called on it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If the intent is in fact the opposite of ucwords(), a simple transform would probably have superior performance.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Nice.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

To avoid a one line function littering the namespace.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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..

Code: Select all

static $function; if (!$function)
...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
Post Reply