1

End goal: Changing chunks of numbers in a database like 0000000 and 22222 and 333333333333 into different lengths. For example, phone numbers that may include prefixes like country codes (e.g. +00 (000) 000-0000, or a pattern of 2, 3, 3, 4), so the option of being able to change the length depending on a different phone number format would be nice.

I love the idea of using explode or chunk_split for the simplicity, as I will be processing a lot of data and I don't want it to drain the server too much:

$string = "1111111111";     
$new_nums = chunk_split($string, 3, " ");
echo $new_nums;

^ Only problem here is that I can only use one digit for the length.

What's the best way to go about it? Should I create a function?

2 Answers2

2

With regex?

$regex = "/(\\d{2})(\\d{3})(\\d{3})(\\d{4})/"; 
$number = "111111111111"; 
$replacement = "+$1 ($2) $3-$4"; 

$result = preg_replace($regex, $replacement, $number);

https://regex101.com/r/gS1uC1/1

Petah
  • 44,167
  • 27
  • 153
  • 209
2

Although you mention a phone number in your example it sounds like you wanted something that could do more while also being allowed to specify multiple and variable chunk lengths. One way to do this is to create a function that takes your data, delimiter/separator character and a number of chunk values.

You could make use of func_num_args() and func_get_args() to figure out your chunk lengths.

Here is an example of such a function:

<?php

function chunkData($data, $separator)
{
    $rebuilt = "";
    $num = func_num_args();
    $args = func_get_args();
    if($num > 2)
    {
        for($i = 2; $i < $num; $i++)
        {
            if(strlen($data) > 0)
            {
                $string = substr($data, 0, $args[$i]);
                $segment = strpos($data, $string);
                if($segment !== false)
                {                   
                    $rebuilt .= $string . $separator;
                    $data = substr_replace($data, "", 0, $args[$i]);
                }
            }
        }
    }
    $rebuilt .= $data;
    $rebuilt = rtrim($rebuilt, $separator);
    return $rebuilt;
}

//Usage examples:

printf("Phone number: %s \n", chunkData("2835552093", "-", 3, 3, 4));
printf("Groups of three letters: %s \n", chunkData("ABCDEFGHI", " ", 3, 3, 3));
printf("King Roland's passcode: %s \n", chunkData("12345", ",", 1, 1, 1, 1, 1));
printf("Append leftovers: %s \n", chunkData("BlahBlahBlahJustPlainBlah", " ", 4, 4, 4));
printf("Doesn't do much: %s \n", chunkData("huh?", "-"));

?>

//Output:

Phone number: 283-555-2093
Groups of three letters: ABC DEF GHI 
King Roland's passcode: 1,2,3,4,5 
Append leftovers: Blah Blah Blah JustPlainBlah 
Doesn't do much: huh? 
Crackertastic
  • 4,893
  • 2
  • 28
  • 37
  • Wow, this is incredibly helpful. chunk_data as you used it definitely gave me what I want. I highly appreciate the number of dynamic options you gave me, and they all seem to work very well without overloading the server. Thanks so much! :) – rojascorrine Dec 18 '14 at 14:56
  • Not a problem, glad it helped! I kept regex out of it for the performance purposes. – Crackertastic Dec 18 '14 at 15:10
  • @Crackertastic what performance purposes? Regex is way faster in terms of performance... https://3v4l.org/ZLfQj/perf#output https://3v4l.org/lZtjq/perf#output – Petah May 28 '21 at 09:23
  • 1
    @Petah Honestly, I don't have a great answer. Evidently, when I wrote that comment six and a half years ago I had an incorrect assumption on regex and performance. *Shrugs shoulders.* Thanks for the performance comparisons though - they're insightful! – Crackertastic May 28 '21 at 22:19
  • 1
    @Crackertastic all good, I only looked at this because someone bumped my 6 year old answer too. But for any future passers by, worth noting regex can actually be pretty performant. – Petah Jun 10 '21 at 10:10