循环遍历mysql数据库记录并更改电话格式
我正在处理一个有电话号码的旧数据库表。字段,
但问题是所有条目(超过 4k)的
格式各不相同。
我想知道是否有一种快速方法可以通过使用 PHP 循环记录并将记录更新为特定的电话格式来修复它们
I'm working on a legacy database table that has a phone no. field
but the problem is that all the entries (over 4k) are in varying
formats.
I was wondering if there was a quick way to fix them by looping through the records using PHP and updating the records to a particular phone format
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
4K 听起来根本不像很多唱片。
我敢打赌,不同的格式可以分为有限数量的组合。
我想知道是否可以通过一些明智的 SQL 查询来实现。如果您的 RDBMS 具有正则表达式和字符串替换功能,您可以使用一些 UPDATE 指令来管理它。如果没有,任何能够查询数据库、进行字符串替换和更新的语言都可以完成这项工作。
4K doesn't sound like many records at all.
And I'd bet that the varying formats fall into a finite number of combinations.
I wonder if it'd be possible with a few judicious SQL queries. If your RDBMS has regular expressions and string replacement functions you could manage it with a few UPDATE instructions. If not, any language with the capability of querying a database, doing string replacement, and updating would do the job.
我同意 4k 记录没有什么可担心的。我建议查询电话号码和表的主 ID,从电话号码中删除所有字符,然后将其处理为您想要的格式。或者,您可以将其保留为仅数字,并在每次显示数字时使用前端修改该数字。下面是一些未经测试的脚本,您可以尝试使用。不处理分机并且预计有 10 个号码。
I agree 4k records isn't anything to worry about. I suggest querying the phone numbers and the primary id of the table, stripping all characters from phone number and then manipulating it to be the format you want. Or, you could keep it as only numbers and use your front-end to modify the number every time you display it. Below is a little untested script you can try to use. Doesn't handle extensions and expects there are 10 numbers.