[英]Make a string with 5 php variables
只是认为我们需要回显类似于此的字符串
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";
}
1.将所有变量放在单个数组变量中作为这样的elemnet 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;
第一步:将所有变量放在一个数组中,并使用empty()函数过滤掉空的false变量。
$values=array("value1", "", "value3", '',"value4", "", "value6", '');
$str ='';
$arrNew=array();
foreach($values as $v){
if(! empty($v)) $arrNew []=$v;
}
第二步:迭代通过新数组并在模数命令的帮助下设置换行符,在每个不均匀的循环计数器号后,除数字零之外。
for( $i=0; $i<count($arrNew); $i++){
if( ($i % 2 !== 0) && ($i !== 0) ) {
$str .='.</br>';
}else{
$str .=',';
}
}
echo $str;
采用:
<?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;
?>
这段代码应该可行,但不是很好。 可能有“更好”的解决方案
$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
尝试这个
$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;
尝试为各个位置创建单独的变量并为其分配所需的值,而不是尝试显示原始变量本身的值。
我仍然是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].'.';
请注意,这不会删除,
和.
当各个字符串不存在时。 虽然很容易做到这一点,我认为在答案中包含它是多余的。
问候,
rktcool :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.