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
  • 1
    the short answer: regular-expressions Commented Dec 17, 2014 at 23:34
  • You can check an example on how to use regular expressions to parse/format a phone number in this SOF question stackoverflow.com/questions/4708248/… Commented Dec 17, 2014 at 23:35

2 Answers 2

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? 
Sign up to request clarification or add additional context in comments.

3 Comments

@Crackertastic what performance purposes? Regex is way faster in terms of performance... 3v4l.org/ZLfQj/perf#output 3v4l.org/lZtjq/perf#output
@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 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.
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

1 Comment

For anyone wondering, if you need alfanumeric chars instead of only numbers, use w instead of d on the $regex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.