在php中添加前导0
发布时间:2020-12-13 16:29:25 所属栏目:PHP教程 来源:网络整理
导读:我有 教程1如何制作这个 教程21如何制作这个 教程2如何制作这个 教程3如何制作这个 我需要 tutorial 01如何制作这个 教程21如何制作这个 教程02如何制作这个 教程03如何制作本 所以我可以正确地订购它们. (找到单个数字时添加前导0) 什么是转换的PHP方法?
我有
>教程1如何制作这个 我需要 > tutorial 01如何制作这个 所以我可以正确地订购它们. (找到单个数字时添加前导0) 什么是转换的PHP方法? 提前致谢 请注意 – 请确保它首先识别单个数字,然后添加前导零
如果它来自数据库,这是在sql查询上执行此操作的方法:
lpad(yourfield,(select length(max(yourfield)) FROM yourtable),'0') yourfield 这将获得表中的最大值并放置前导零. 如果是硬编码(PHP),请使用str_pad() str_pad($yourvar,$numberofzeros,"0",STR_PAD_LEFT); 这是我在在线php编译器上所做的一个小例子,它有效…… $string = "Tutorial 1 how to"; $number = explode(" ",$string); //Divides the string in a array $number = $number[1]; //The number is in the position 1 in the array,so this will be number variable $str = ""; //The final number if($number<10) $str .= "0"; //If the number is below 10,it will add a leading zero $str .= $number; //Then,add the number $string = str_replace($number,$str,$string); //Then,replace the old number with the new one on the string echo $string; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |