php字符串形成

时间:2011-03-07 16:07:48

标签: php string format

我有一个非常简单的问题,对我来说不是简单的,因为我是学生,但我必须从某个地方开始,所以问题是我有一个字符串数组

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

我如何格式化输出字符串,例如在我想要的浏览器中:

+ 9 %
+12 %
+ 1 %

向你寻求帮助。

7 个答案:

答案 0 :(得分: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])); 

答案 1 :(得分:1)

假设输出中的空格是拼写错误

您可以使用foreach()循环来迭代aray

$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>";
}

以上代码非常随意,仅适用于您的阵列,但我看到了更奇怪的家庭作业。

答案 2 :(得分:0)

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


?>

答案 3 :(得分:0)

我的两分钱:

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

答案 4 :(得分: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>';

    }

}

?>

答案 5 :(得分:0)

作为一个使用负数和零的解决方案,也更紧凑:

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

答案 6 :(得分: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>';