Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2005-06-12 01:34:32

Andrew
Plugin Author
Registered: 2004-02-23
Posts: 730

help: sprintf & plugins

I am creating a plugin that is using sprintf and a “format” parameter so that the user can override the default language & formatting settings. For example:

<span style=“line-height:20px”>
  • ‹txp:abc_plugin format="There are %d monkeys in the %s" /› => There are 10 monkeys in the trees
  • ‹txp:abc_plugin format="The %2\$s contain %1\$d monkeys" /› => The trees contain 10 monkeys
</span>

In the first example it works fine, but for some reason in the second example, when passing the format as a parameter, sprintf doesn’t like the value and only outputs “The $s contains $d monkeys”, rather than parsing the string and replacing the values. I’ve tested to make sure the function would work correctly in all other circumstances and it definitely has something to do with what is received by sprintf that it doesn’t like. The plugin function appears to be receiving the correct string format as a parameter.

Do any php geeks out there have any idea what could be going wrong? Is this a datatype issue?

Last edited by Andrew (2005-06-12 01:40:34)

Offline

#2 2005-06-12 01:49:03

Andrew
Plugin Author
Registered: 2004-02-23
Posts: 730

Re: help: sprintf & plugins

As a more specific example…

Tag used in form: ‹txp:abc_plugin format="The %2\$s contain %1\$d monkeys" /›

(and now from within the plugin function)

<pre>
function abc_plugin($atts) {
...
echo $format;
echo sprintf($format,10,'trees');
echo sprintf("The %2\$s contains %1\$d monkeys",10,"trees");
...
}
</pre>

Result:

The %2\$s contains %1\$d monkeys
The $s contains $d monkeys
The trees contains 10 monkeys

Offline

#3 2005-06-12 01:58:55

Andrew
Plugin Author
Registered: 2004-02-23
Posts: 730

Re: help: sprintf & plugins

It’s so strange because the two strings are identical, yet md5’ing them yields different results and they are not equal via ==. is_string() checks out for both… copying and pasting show the same strings… is some character getting converted?

Offline

#4 2005-06-12 02:06:25

Andrew
Plugin Author
Registered: 2004-02-23
Posts: 730

Re: help: sprintf & plugins

strcmp("The %2\$s contains %1\$d monkeys", $format) => -56
$format => "The %2\$s contains %1\$d monkeys"

?!

Offline

#5 2005-06-12 02:08:51

Andrew
Plugin Author
Registered: 2004-02-23
Posts: 730

Re: help: sprintf & plugins

Ok, so:

var_dump("The %2\$s contains %1\$d monkeys") => string(30)
var_dump($format) => string(32)

It’s gotta be something about the backslashes…

Last edited by Andrew (2005-06-12 02:09:49)

Offline

#6 2005-06-12 02:12:19

Andrew
Plugin Author
Registered: 2004-02-23
Posts: 730

Re: help: sprintf & plugins

GOT IT

Phew. So you’ve got to leave out the escaping backslash if using this format via plugin parameter, such as:

‹txp:abc_plugin format="The %2$s contain %1$d monkeys" /›

Man, it’s lonely talking to oneself…

Offline

#7 2005-06-12 02:21:14

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: help: sprintf & plugins

But amusing to the eavesdropper. ;p

Offline

#8 2005-06-12 02:22:16

Andrew
Plugin Author
Registered: 2004-02-23
Posts: 730

Re: help: sprintf & plugins

yer just a bunch of monkeys

Offline

#9 2005-06-12 02:33:05

sekhu
Member
Registered: 2005-05-12
Posts: 428
Website

Re: help: sprintf & plugins

lol was worth reading…

tomorrow on the TXP comedy hour :)

Offline

#10 2005-06-12 17:06:42

Manfre
Plugin Author
From: North Carolina
Registered: 2004-05-22
Posts: 588
Website

Re: help: sprintf & plugins

This thread was quite amusing. Getting a rare glimpse at some ones “internal” voice. :P

Offline

Board footer

Powered by FluxBB