使用下一个代码,我可以用图像更改号码(我不想让谷歌知道这个电话号码)。是否可以每 3 个数字/图像插入空格?
比如手机是111222333,我要在前面显示111 222 333。
<?php global $wp_query;
$postid = $wp_query->post->ID;
$i=intval(get_post_meta($postid, 'telefono_2', true));
$s='';
while($i>0)
{
$digit=$i % 10;
$i=(int)($i/10);
$s="<img style='vertical-align:middle;' src='http://localhost/buscatuescort/wp-content/themes/Avada/images/$digit.png' width='12' height='18' />$s";
}
echo $s ?>
最佳答案
您只需添加某种计数器 并在循环的每 3 个迭代步骤中向字符串添加一个空格。也许是这样的:
$spacer=0;
while($i>0)
{
$digit=$i % 10;
$i=(int)($i/10);
$s="<img style='vertical-align:middle;' src='http://localhost/buscatuescort/wp-content/themes/Avada/images/$digit.png' width='12' height='18' />"
. ($spacer++ % 3 == 0 ? " " : "") . $s;
}
备注:
当您从最后一位数字向后迭代时,如下所示:
11122233
会像这样打印出来:
11 122 233
但是如果你想得到这样的东西:
111 222 33
您将不得不编写一个略有不同的循环。也许是这样的:
$i = "$i";
$length = strlen($i);
for($key = 0; $key < $length; $key++){
$s .= ($key % 3 == 0 ? " " : "")
. "<img style='vertical-align:middle;' src='http://localhost/buscatuescort/wp-content/themes/Avada/images/$i[$key].png' width='12' height='18' />";
}
(我将 $i
更改为字符串以循环遍历数字,就像这样 $i[$key]
)
关于php - 在 jpeg 数字之间插入空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19625779/