AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MalaysianPhone.php 1.1KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace App\Support;
  3. /**
  4. * Turns Malaysian phone numbers as people type them ("012-345 6789", "+60 12 345 6789")
  5. * into the digits-only form WhatsApp links need ("60123456789").
  6. */
  7. final class MalaysianPhone
  8. {
  9. public static function normalize(?string $phone): ?string
  10. {
  11. $digits = preg_replace('/\D/', '', (string) $phone) ?? '';
  12. if (str_starts_with($digits, '0')) {
  13. // Local form: 012-345 6789.
  14. $digits = '6'.$digits;
  15. } elseif (str_starts_with($digits, '1') && strlen($digits) >= 9 && strlen($digits) <= 10) {
  16. // Mobile number typed without the leading 0: 12-345 6789.
  17. $digits = '60'.$digits;
  18. }
  19. // Anything else must already carry Malaysia's country code (60). Other countries are refused.
  20. return preg_match('/^60[1-9][0-9]{7,9}$/', $digits) === 1 ? $digits : null;
  21. }
  22. /**
  23. * A WhatsApp chat link, optionally with a message already typed in.
  24. */
  25. public static function whatsappLink(string $number, ?string $text = null): string
  26. {
  27. return 'https://wa.me/'.$number.($text !== null ? '?text='.rawurlencode($text) : '');
  28. }
  29. }