如何正确显示两个字符串并排

时间:2015-10-27 10:14:37

标签: php html css arrays string

我有两个数组,我使用implode()来转换成字符串然后被回显。

理想情况下,我希望一个字符串中的1值与另一个字符串中的另一个值相邻,优先嵌套在

<p></p>

这是我用来在每个值之后发出换行符的代码,将它包装在div中,然后浮动它。

<?php 
echo '<div class="wrapper"><div style="text-align:left; float:left;"         
class="glue">';
echo implode('<br>',$test);
echo '</div>';
echo '<div style="text-align:right; float:right;" class="glue">';
echo implode('<br>',$_POST);
echo '</div></div>';
?>

现在效果很好,因为两个div在我的容器中彼此相邻但是如果值在P标签内部会更容易,所以我可以轻松地将图像放在它们之间。

我在下面添加了一些图片,以便更好地展示我的目标。 第一个是我尝试过的,第二个是我想要的。

enter image description here enter image description here

3 个答案:

答案 0 :(得分:1)

在将段落标记分成两半之前,这是不可能的。

试试这个,

.div1 {
    float: left;
}

.div2 {
    float:right;
}

.div2 {
    float:right;
    text-align: right;
}
<p>
    <div class="div1">Left Text</div>
    <div class="div2">Right Text</div>
</p>

答案 1 :(得分:1)

这是你想要的吗?

<style type="text/css">
    p{
        overflow: hidden;
    }
    span.left{
        float: left;
    }
    span.right{
        float: right;
    }
</style>
<?php
foreach($test as $k=>$v){
    echo '<p><span class="left">'.$test[$k].'</span><span class="right">'.$_POST[$k].'</span></p>';
}

答案 2 :(得分:1)

如果两个数组的长度相同,那么最简单的示例如下所示:

<?php 
$test[0] = "a";
$test[1] = "b";
$test[2] = "c";
$test[3] = "d";
$test2[0] = 1;
$test2[1] = 2;
$test2[2] = 3;
$test2[3] = 4;
for($i=0;$i<count($test);$i++)
{
echo "<p><nobr>".$test[$i] . "</nobr> <nobr style='float:right;'>" . $test2[$i] . "</nobr></p>";
}

如果你想测试它是否适合只粘贴代码here