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”。我怎样才能正确地做到这一点?
解决方案
你需要在空间的基础上分解你的字符串,然后 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;
推荐阅读
- c# - 如何在这段代码中用 do-while 替换 while?
- postgresql - 在另一个表中插入一个表中的值作为外键
- javascript - Javascript 根据值从下拉列表中获取选项
- c - 从 8 位到 32 位的位复制
- php - PHP 和 MySQL,使用外键从另一个表中获取数据
- android - 使用 Constraintlayout 和 dp,实际设备中布局大小不同
- java - @CreationTimestamp 和 @UpdateTimestamp 不适用于 JPA 2.0.2
- c# - 如何为 Swagger UI 定义参数的默认值?
- arrays - 添加新值时使用 onEdit(e) 更新单元格
- excel - 在 Sub VBA 中调用用户定义的函数