ICStudio
Services
PortfolioCase Studies
Tools
DemoBlog
ICStudio
HomeBlogPhone number normalizer

Phone number normalizer

A common task in working with client projects is to normalize phone numbers. Customers on the sites can enter the phone number in international format, in local format, with or without brackets, with or without a plus in the beginning.

Published: 10/25/2021

Досить частою задачею в роботі з клієнтськими проектами є нормалізація номерів телефонів. Замовники на сайтах можуть вводити телефон у міжнародному форматі, у локальному, з дужками, без дужок, з плюсом на початку чи без.

Наша Студія розробила свій універсальний метод для українських телефонних номерів, який дозволяє привести номер телефону до локального або міжнародного формату: або 0991234567 або +380991234567

Якщо Ви пишете на Laravel або схожих системах, цей медод можна додати до helpers.php як допоміжний.

php
function normalize_phone_number($s, $internation = true)
    {
        $s = trim($s);
        $s = preg_replace('/[^0-9]/', '', $s);
        $s = preg_replace('/^[+]?380/', '0', $s);
        if (strlen($s) === 11 && substr($s, 0, 1) === '8') $s = substr($s, 1);
        if (strlen($s) === 11 && substr($s, 0, 2) === '38') $s = '0' . substr($s, 2);
        if (strlen($s) === 12 && substr($s, 0, 2) === '38') $s = substr($s, 2);
        
        if (!$internation) {
            return $s;
        }
        
        return $s = '+38' . $s;
        
    }

Давайте протестуємо:

php
echo normalize_phone_number('380(99)123-45-678');
		+3809912345678
		echo normalize_phone_number('380(99)123-45-678', false);
		09912345678

Відтепер хоча б для українських телефонів можна не страждати 😄

Share:

Want to work with us?

Let's discuss your project and find the best solution

Contact us

Related Posts

10/16/2024

Why October CMS is the ideal choice for Creative Businesses and Businesses with non-standard business processes

10/14/2024

Online Payment Security: How to Protect Client Data in Your Laravel Project

5/17/2023

Pawnshop software: benefits, features, requirements

ICStudio

Professional web development and IT solutions for business

Quick Links

  • Home
  • Portfolio
  • Case Studies
  • Blog
  • Team

Resources

  • Tools
  • AI Chatbots
  • Demo
  • Docs
  • Pilot Program
  • Hire Us

Services

  • Web Development
  • E-commerce
  • CRM
  • Support

Contacts

  • hello@icstudio.online
  • +38 099 123 45 67
  • Kyiv, Ukraine

© 2026 IC Studio. All rights reserved.

Privacy PolicyTerms of ServiceService Terms