Simple Machines Blogs > Developers' Blog

PHP String Replacement Speed Comparison

<< < (5/6) > >>

Joshua Dickerson:
sprintf() isn't really in the same boat as the other string replacement functions IMO. It is more of a fancy concatenation and in that boat, it isn't so fast. If you have a string that looks like your sprintf() string and you want to return or output it, use a language construct (double quotes, dots, commas, etc) instead.

Thantos:
There is of course one major problem with doing that:  It requires you to determine the language grammer in the source and not in the language strings.

Lets take the following statement:

--- Code: ---My name is {NAME} and I am a {JOB} here at {COMPANY} where I work for {BOSSTITLE} {BOSS}
--- End code ---
example:  My name is Thantos and I am a developer here at Simple Machines where I work for Lead Developer Compuart

Now if I wanted to that with concatation (of any sort) it would be something like: ($t is an array of language strings)

--- Code: ---$t[1] . $name . $t[2] . $job . $t[3] . $company . $t[4] . $bosstitle . ' ' . $boss
--- End code ---
So if I wanted to that sentence I'd need to use four text strings.  But now lets say for my particular setting "Lead Developer Compuart" isn't proper, I need "Compuart the Lead Developer".  Not only have I inserted another text string ("the") but I have rearranged the order in which the data appears.  No amount of changing of (just text strings)
--- Code: ---$t[1] . $name . $t[2] . $job . $t[3] . $company . $t[4] . $bosstitle . ' ' . $boss
--- End code ---
is going to get me
--- Code: ---My name is Thantos and I am a developer here at Simple Machines where I work for Compuart the Lead Developer
--- End code ---

Now if I put it all into one text string and use some some sequence identifiers I can do this:

--- Code: ---My name is %1$s and I am a %2$s here at %3$s where I work for %4$s %5$s
--- End code ---
and call it like: $str = sprintf($text, $name, $job, $company, $bosstitle, $boss);
Now if they want to make the change I did then it is a simple edit job to the language string

--- Code: ---My name is %1$s and I am a %2$s here at %3$s where I work for %5$s the %4$s
--- End code ---

Heck you could even leave the boss title out and have

--- Code: ---My name is %1$s and I am a %2$s here at %3$s where I work for %5$s
--- End code ---

One thing to consider is if you want to use

--- Code: ---My name is %1$s and I am a %2$s here at %3$s where I work for %4$s %5$s
--- End code ---
and gain the speed or use

--- Code: ---My name is {NAME} and I am a {JOB} here at {COMPANY} where I work for {BOSSTITLE} {BOSS}
--- End code ---
and gain the readability.

That choice depends mostly on how the string is being used.

Joshua Dickerson:
What's wrong with the following:
$str  = "My name is $NAME and I am a $JOB here at $COMPANY where I work for $BOSSTITLE $BOSS";

Thantos:
Can you export that into a language file so it can be translated into other languages?

What happens if you load the file that contains the string and those variables aren't defined yet?

Joshua Dickerson:

--- Quote from: groundup on June 12, 2007, 04:15:16 PM ---$txt['no_sprintf']  = "My name is $GLOBALS[NAME] and I am a $GLOBALS[JOB] here at $GLOBALS[COMPANY] where I work for $GLOBALS[BOSSTITLE] $GLOBALS[BOSS]";

--- End quote ---
or something similar to that. As for not being defined, okay, I guess there is where you have the difference. Although, %1$s can hardly be considered readable.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version