2011-03-07 33 views
1

我有一個非常簡單的問題,對我來說並不簡單,因爲我是個學生,但地方我要開始,所以這個問題是我有一個字符串數組PHP串成形

array("+9%","+12%","+1%") 

如何我可以格式化輸出字符串,例如在我想要的瀏覽器中:

+ 9 % 
+12 % 
+ 1 % 

Thans for your help。

回答

2

看一看這個頁面:

http://ch2.php.net/manual/fr/function.sprintf.php

它應該是這樣的:

$values = array("+9%","+12%","+1%"); 
echo sprintf ("+%2d \n+%2d \n+2%d", intval($values[0]), intval($values[1]), intval($values[2])); 
+0

將代碼塊添加到您的代碼 – JohnP 2011-03-07 16:23:38

+0

謝謝,我的第一個答案在這裏...我應該更仔細地看待。 – algorithme 2011-03-07 16:25:48

0
<? 
foreach($yourarray as $element) { 
    echo $element."<br />"; 
} 


?> 
+0

我認爲他想也排隊單位。所以9,2和1都排隊。 – Endophage 2011-03-07 16:17:40

1

假設輸出的空間是一個錯字

可以使用foreach()遍歷阿雷

$myArray = array("+9%","+12%","+1%"); 
foreach ($myArray as $elem) { 
    echo $elem . '<br>'; //BR is for breaks in the browser. 
} 

迭代如果空間是不是筆誤,它得到有點棘手

$myArray = array("+9%","+12%","+1%"); 
foreach ($myArray as $elem) { 
    $sign = $elem[0]; //gets the first element of the string treated as an array 
    $number = substr($elem, 1, strpos($elem, '%') - 1); //gets the number part by starting from the left and going till you hit a % sign 
    $percent = $elem[strlen($elem) - 1]; //getting the last part of the string 
    echo "{$sign} {$number} {$percent} <br>"; 
} 

上面的代碼是相當隨意的,只適用於你r數組,但我已經看到了更多的家庭作業。

+0

上面的代碼很漂亮嗎? – mellamokb 2011-03-07 16:18:39

+0

更多漂亮的是'str_pad()';) – powtac 2011-03-07 16:20:04

+0

@mellamokb ass的用法?螞蟻?高手?猿? – JohnP 2011-03-07 16:20:54

0

我的兩分錢:

foreach ($data as $entry) { 
    echo '+'.str_pad(trim(str_replace(array('+', '%'), $entry)), 2).' %'; 
} 
0

有時回答這些需要注意的告誡,所以這裏是我的。

[1]我假定數組中的所有值都是百分比,所以我將它從我的起始數組中刪除,並在打印字符串時附加它們。

[2]我在每個元素的開頭都允許有正號或負號。

[3]我期待的數值是一個整數(我假設你想要的比對,你在你的問題,每個值佔用兩個空格有)

如果有任何的這些假設是不正確的,下面的代碼需要修改以解釋變化。

<?php 

$arrPercentages = array('+9','+12','+1'); 

foreach($arrPercentages as $strPercentage) { 

    // Get rid of any spaces 
    $strPercentage = str_replace(' ', '', $strPercentage); 

    // Parse out the number 
    preg_match('/([\-\+]{1})([0-9]+)/', $strPercentage, $arrMatches); 

    // Don't continue if we didn't get a sign and number out of the string 
    if(count($arrMatches)) { 

     // Assign the "sign" to a variable 
     $strSign = $arrMatches[1]; 

     // The number we want to print out as two character spaces 
     $strNumber = $arrMatches[2]; 

     // Print it out! 
     echo $strSign.sprintf("%2s", $strNumber).'%<br>'; 

    } 

} 

?> 
0

作爲與負數的工作和零的,也是一個解決辦法是更緊湊:

$arr = array("+9%","+12%","+1%"); 
foreach($arr as $num) { 
    echo preg_replace("~^([+-]?)(\d+)%$~e", "sprintf('%1s%2s %%', '$1', $2)", $num)."<br/>"; 
} 
0

過頂;-)了一下:

$array = array(
    '+196%', 
    '+ 12%', 
    '- 16 pct', 
    '-84 %' 
); 

$rows = array(); 
foreach($array as $element) 
{ 
    preg_match('/(?<sign>[-+])?\s?(?<number>[0-9]+)?\s?(?<remaining>.*)/', $element, $matches); 
    $rows[] = '<div><span class="sign">' . $matches[ 'sign' ] . '</span><span class="number">' . $matches[ 'number' ] . '</span><span class="remaining">' . $matches[ 'remaining' ] . '</span></div>'; 
} 

echo ' 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Rows of data</title> 
<style type="text/css"> 
span { 
    float: left; 
} 
.sign { 
    clear: left; 
    width: 1ex; 
} 
.number { 
    width: 4ex; 
    padding-right: 1ex; 
    text-align: right; 
} 
</style> 
</head> 
<body> 
' . implode(PHP_EOL, $rows) . ' 
</body> 
</html>';