5

试想我们需要回显一个类似于此的字符串

sring01, sring02.<br />
sring03, sring04.</br />
sring05. 

所有字符串都来自变量。所有五个变量都具有真实值并不重要。如果他们有错误或空输出字符串应该与上面不同。假设我们有 2 个空变量用于 string02 和 string03,那么输出应该是

sring01, sring04.</br />
sring05.

谁能告诉我实现这一目标的最佳方法是什么?

我只是尝试过这样的事情,但如果不是所有变量都为真,它对我不起作用。

if($addressOne||$addressTwo||$city||$province||$country) {
    $location  = "$addressOne, $addressTwo.<br />";
    $location .= "$city, $province.<br />";
    $location .= "$country";
} else {
    $location = "some text";
    }
4

6 回答 6

8

1.将所有变量放在一个数组变量中,像这样的元素 2.然后使用此数组函数过滤数组以获取错误值 3.然后使用过滤后的数组分块

$array = array($var1, $var2, $var3, $var4, $var5); //place all variables in an array

$filtered = array_filter($array); //Filter all false values such as '', null, FALSE

$chunk = array_chunk($filtered, 2); //Chunk whole array to smaller groups with 
                                    //atmost 2 elements
$data = '';
   foreach($chunk as $value)
   {
      $data .= implode(',', $value) . '<br/>';  //Then join two elements with ',' symbol
   }
echo $data;
于 2013-09-07T07:27:50.650 回答
2

第一步:将所有变量放入一个数组中,并使用 empty() 函数过滤掉空的、假的。

$values=array("value1", "", "value3", '',"value4", "", "value6", '');

$str ='';
$arrNew=array();
foreach($values as $v){
    if(! empty($v)) $arrNew []=$v;
}

第二步:遍历新数组,并在 modulo 命令的帮助下设置一个换行符,在每个奇偶循环计数器编号之后,除了数字零。

for( $i=0; $i<count($arrNew); $i++){
    if( ($i % 2 !== 0) && ($i !== 0) ) {
        $str .='.</br>';
    }else{
        $str .=',';
   }
}


echo $str;
于 2013-09-07T07:23:05.250 回答
1

利用:

<?php
$addressOne = "string1";
$addressTwo = "";
$city = "";
$province = "string4";
$country = "string5";

if(!empty($addressOne)) $string[] = $addressOne;
if(!empty($addressTwo)) $string[] = $addressTwo;
if(!empty($city))       $string[] = $city;
if(!empty($province))   $string[] = $province;
if(!empty($country))    $string[] = $country;

$str = "";
for($i=0; $i<count($string); $i++) {

    $str .= $string[$i];
    $str .= $i%2==0 ? "," : ".<br>"; 

}
$str = trim($str,",");
$str .= ".";

echo $str;
?>
于 2013-09-07T07:27:07.647 回答
0

这段代码应该可以工作,但它不是很好。可能有“更好”的解决方案

$location = "";
$count = 0;

// The first string can be set if not empty
if($string01) {
  $location = $string01;
  $count++;
}

// second string 
if($string02) {

  // If $location is not empty we know the first was set and so we are have to add a "<br>"
  if($location != "") {
      $location .= "," . $string02 . "<br />";
      $count = 0;
    }
    else
    {
      $location = $string02;
      $count++;
    }
}

// For String 3 to 5 we have to check what our count is. when it is 0 then we don't have to add a <br> else we have because the string is the second in line
if($string03) {
  if($location != "") {
      if($count == 0) {
        $location .= $string03;
        $count++;
      }
      else
      {
        $location .= "," . $string03 . "<br />";
        $count = 0;      
      }
    }
    else
    {
      $location = $string03;
      $count++;
    }
}


if($string04) {
  if($location != "") {
      if($count == 0) {
        $location .= $string04;
        $count++;
      }
      else
      {
        $location .= "," . $string04 . "<br />";
        $count = 0;      
      }
    }
    else
    {
      $location = $string04;
      $count++;
    }
}

// For the last string we don't have to reset count because it's the last
if($string05) {
  if($location != "") {
      if($count == 0) {
        $location .= $string05;
      }
      else
      {
        $location .= "," . $string05 . "<br />";   
      }
    }
    else
    {
      $location = $string05;
    }
}

// Final text in $location
于 2013-09-07T07:16:20.587 回答
0

try this

$addressOne='ome';
$province='prov';
$country='$country';

if(!empty($addressOne)||!empty($addressTwo)||!empty($city)||!empty($province)||!empty($country)) {
    $locs = array();
    if (!empty($addressOne))$locs[]=$addressOne;
    if (!empty($addressTwo))$locs[]=$addressTwo;
    if (!empty($city))$locs[]=$city;
    if (!empty($province))$locs[]=$province;
    if (!empty($country))$locs[]=$country;
    $l = array();
    for ($i=0;$i<count($locs);$i+=2){
        $c=$locs[$i];
        if (!empty($locs[$i+1]))$c.=", {$locs[$i+1]}";
        $l[]=$c;
    }
    $location = implode('<br/>',$l);
}else{
    $location = "some text";
}
echo $location;
于 2013-09-07T07:19:49.660 回答
0

尝试为各个位置创建单独的变量并为其分配所需的值,而不是尝试显示原始变量本身的值。

我还是PHP的新手,如有错误或错误请指出:)

如果假设我认为位置变量如下:

pos1, pos2.<br/>
pos3, pos4.<br/>
pos5.

那么以下可能会起作用:

$pos = array ($addressOne, $addressTwo, $city, $province, $country);
for ($i=0; i<5; $i++)
  {
  if(!$pos[$i])
    {
    $j=$i;
    while($pos[$j+1])
      {
      $pos[$j] = $pos[$j+1];  //shift the strings one position to the front
      $j++;
      }
    }
  }
echo $pos[0].', '.$pos[1].'.<br/>'.
     $pos[2].', '.$pos[3].'.<br/>'.
     $pos[4].'.';

请注意,当相应的字符串不存在时,这不会删除,and 。.不过,这样做很容易,我认为将其包含在答案中是多余的。

问候,
rktcool:)

于 2013-09-07T07:27:54.103 回答