2012-01-19 35 views
4

有沒有人知道更好的執行sprintf in PHP?我一直在尋找類似的字符串格式化,我們在蟒蛇:改進sprintf爲PHP

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構建廣泛的查詢,那麼這有一個特別有趣的用法... ON DUPLICATE KEY UPDATE語法..._ [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-parsing-in-php and 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指的是第二個等)。正如您在上面看到的,當您使用位置綁定的佔位符時,可以在字符串內重複它們,而無需在調用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); 

上述兩個表達式是等效的,它們都將輸出

「昨天,我看到了一隻貓,這是一個毛茸茸的貓!我從來沒見過貓這麼蓬鬆。」

+0

感謝您的幫助! –

2

我回答在另一篇文章過這個問題:vsprintf or sprintf with named arguments, or simple template parsing in PHP

但是,這您是在尋找相同的格式!

這真的是最好的方式去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!" 

多維數組值被允許了。希望可以幫助。