2012-01-19 9 views
4

sprintfPHPの方がよいでしょうか?私たちはPythonで持っている文字列フォーマットのようなものを探していた。PHPのsprintfが改善された

print "Hello %(name)s. Your %(name)s has just been created!" % { 'name' : 'world' } 
# prints::: Hello world. Your world has just been created! 

これは、などを必要とせず同じ変数、繰り返しを避けるためにはかなり便利です:私は推測

sprintf("Hello %s. Your %s has just been created!", 'world', 'world'); 
# prints::: Hello world. Your world has just been created! 

はに非常に簡単ですこれを私自身で構築しますが、私の言いたいことを知っていれば、ホイールを再発明したくないですが、どこでもこのトレースを見つけることができませんでした。

誰かが助けることができれば、私は感謝します。

乾杯、

+1

これは、MySQLの_INSERT ...で重大なクエリを作成する場合に特に興味深い使用法です。重複キー更新構文... [http://dev.mysql.com/doc/refman/5.0/en /insert-on-duplicate.html](http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) –

+0

ご覧ください:http://stackoverflow.com/questions/ 5701985/vsprintf-or-sprintf-with-named-arguments-or-simple-tempalte-phpおよびhttp://stackoverflow.com/questions/7435233/name-php-specifiers-in-printf-strings – lqez

答えて

8

あなたは一例

printf('Hello %1$s. Your %1$s has just been created!', 'world'); 

ここで注意の言葉のために、これを行うには、位置(ただし、名前が付けられていない)引数を使用することができます:あなたは単一引用符を使用する必要があり、そうでない場合はドル記号をしようとするPHPの原因となります$sをこの変数の値に置き換えます(存在しません)。

名前付き引数が必要な場合は、正規表現でこれを行う必要があります。例えば、How to replace placeholders with actual values?を参照してください。

+0

すっきり!ありがとう。それはトリックを行う! –

5

あなたは(それはのように見栄えではないかもしれませんが) PHPのsprintfと同じプレースホルダを繰り返すことができます:

$str = sprintf('%1$s %1$s', 'yay'); 
// str: 'yay yay' 

プレースホルダに右%n$を使用することができ、ここでnは引数の位置です(%1$sは最初の引数(文字列として)を参照し、%2$sは2番目の引数などを参照します)。上記のように、位置的にバインドされているプレースホルダを使用する場合は、sprintfを呼び出すときに引数を重複させずに文字列内で繰り返すことができます。

+0

あなたは正しかった...いいものではないが、少なくともトリックを行う。ありがとう! –

3

次のコードはa post by Salathe on TalkPHPから盗まれました。

$szAdjective = 'fluffy'; 
$szNoun = 'cat'; 

printf('Yesterday, I saw a %s. '. 
     'It was a %s %s! I have '. 
     'never seen a %s quite so %s.', 
     $szNoun, 
     $szAdjective, 
     $szNoun, 
     $szNoun, 
     $szAdjective); 

printf('Yesterday, I saw a %1$s. '. 
     'It was a %2$s %1$s! I have '. 
     'never seen a %1$s quite so %2$s.', 
     $szNoun, 
     $szAdjective); 

上記2つの式が等価であり、出力

両方が意志「昨日、私は猫を見た。それは、ふわふわの猫だった!私はそれほどふわふわ猫を見たことがありません。」

+0

助けてくれてありがとう! –

2

私は別の記事で、この非常に疑問答え:vsprintf or sprintf with named arguments, or simple template parsing in PHP

をしかし、これはyoureのを探して同じフォーマットを持っています!

これは実際にimhoに行く最善の方法です。隠された文字はありません。単にキー名を使用してください!私はあなたがPHPの文字列で名前置換を行うことができ、小さなコンポーネントを書いた http://www.php.net/manual/en/function.vsprintf.php

function dsprintf() { 
    $data = func_get_args(); // get all the arguments 
    $string = array_shift($data); // the string is the first one 
    if (is_array(func_get_arg(1))) { // if the second one is an array, use that 
    $data = func_get_arg(1); 
    } 
    $used_keys = array(); 
    // get the matches, and feed them to our function 
    $string = preg_replace('/\%\((.*?)\)(.)/e', 
    'dsprintfMatch(\'$1\',\'$2\',\$data,$used_keys)',$string); 
    $data = array_diff_key($data,$used_keys); // diff the data with the used_keys 
    return vsprintf($string,$data); // yeah! 
} 

function dsprintfMatch($m1,$m2,&$data,&$used_keys) { 
    if (isset($data[$m1])) { // if the key is there 
    $str = $data[$m1]; 
    $used_keys[$m1] = $m1; // dont unset it, it can be used multiple times 
    return sprintf("%".$m2,$str); // sprintf the string, so %s, or %d works like it should 
    } else { 
    return "%".$m2; // else, return a regular %s, or %d or whatever is used 
    } 
} 

$str = <<<HITHERE 
Hello, %(firstName)s, I know your favorite PDA is the %(pda)s. You must have bought %(amount)s 
HITHERE; 

$dataArray = array(
    'pda'   => 'Newton 2100', 
    'firstName' => 'Steve', 
    'amount'  => '200' 
); 
echo dsprintf($str, $dataArray); 
// Hello, Steve, I know your favorite PDA is the Newton 2100. You must have bought 200 
+0

悪くない。共有いただきありがとうございます!私はあなたが言及したように、むしろこれを「暗号」よりも使用したいと思います。 –

0

:PHPのサイトから取ったよう

。それはと呼ばれています。あなたは、このようなコードで欲しいものを得ることができ、それにより :

$engine = new StringTemplate\Engine; 

$engine->render(
    '"Hello {name}. Your {name} has just been created!"', 
    [ 
     'name' => 'world', 
    ] 
); 
//Prints "Hello world. Your world has just been created!" 

多次元配列の値があまりにも許可されています。希望が助けることができる。

関連する問題