如何在锚标签内将数字的字符串值转换为电话格式?

时间:2019-01-17 13:02:45

标签: php

我正在尝试转换数字的动态字符串值,将来可能会更改它,这就是为什么我不想对其进行硬编码的原因。

<?php $telephone = (03) 1234 5678; // this number displays on the frontend only ?>

<a href="tel:+61-3-1234-5678"><?php echo $telephone; ?></a>

所以我想将值从“(03)1234 5678”更改/格式化为“ tel:+ 61-3-1234-5678”。 我该如何正确地做到这一点?

1 个答案:

答案 0 :(得分:3)

您需要根据空间爆炸字符串,然后在str_replace处删除(,)和0,然后找出跟随输出。此代码适用于php。

    $str="tel:+61-";
    $phone ="(03) 1234 5678";
    $string=explode(" ",$phone);
    $strone=str_replace("(","",$string[0]);
    $strone=str_replace(")","",$strone);
    $strone=str_replace("0","",$strone);
    print_r($str.$strone."-".$string[1]."-".$string[2]);
    exit;