首页 > 解决方案 > 如何将数字的字符串值转换为锚标签内的电话格式?

问题描述

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

<?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”。我怎样才能正确地做到这一点?

标签: php

解决方案


你需要在空间的基础上分解你的字符串,然后 str_replace 删除 (,) 和 0 然后你会发现跟随 output.this code for 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;

推荐阅读