我想根据手机号码还是固定电话/家庭电话来设置不同的电话号码格式。澳大利亚的手机号码以04开头;我可以这样做吗:
{if (substr($number, 0,2) == '04'}
apply this format #### ### ###
{else}
apply this format (##) #### #### or #### ###
取决于是否输入了区号。
我把它应用到了一个template/html页面上,我遇到了一些问题。。。。手机号码可以识别和格式化,但其他号码不起作用。。。。这就是我所做的
{if $listing.user[$v.caption]|substr:0:2== '04'}
{$listing.user[$v.caption]|substr:0:4}
{$listing.user[$v.caption]|substr:4:3}
{$listing.user[$v.caption]|substr:7}
{elseif $listing.user[$v.caption]|strlen>8}
({$listing.user[$v.caption]|substr:0:2})
{$listing.user[$v.caption]|substr:2:4}
{$listing.user[$v.caption]|substr:6}
{else}
{$listing.user[$v.caption]|substr:0:4}
{$listing.user[$v.caption]|substr:4:4}
{/if}
您的if语句应该可以完成以下操作:
//if the first two digits are `04` then use the format: #### ### ###
if (substr($number, 0, 2) == '04') {
$number = substr($number, 0, 4) . ' ' . substr($number, 4, 3) . ' ' . substr($number, 7);
//otherwise do more checks on the number
} else {
//if the number is longer than seven digits then use the format: (##) #### ####
if (strlen($number) > 7) {
$number = '(' + substr($number, 0, 2) . ') ' . substr($number, 2, 4) . ' ' . substr($number, 6);
//otherwise use the format: #### ###
} else {
$number = substr($number, 0, 4) . ' ' . substr($number, 4, 3);
}
}