I have 20 digits|length string, example: 22223333333334444333
. How to manipulate with given format, like this: 00-00-000-000-000-0000-000
in PHP?
expected result: 22-22-333-333-333-4444-333
I have 20 digits|length string, example: 22223333333334444333
. How to manipulate with given format, like this: 00-00-000-000-000-0000-000
in PHP?
expected result: 22-22-333-333-333-4444-333
收起
You could use some Regular Expressions to get it done in a few steps.
<?php
// Test string
$string = "22223333333334444333";
// Pattern: 22-22-333-333-333-4444-333
$pattern = "/([0-9]{2})([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{3})/";
// Get all the matching elements in the pattern
preg_match($pattern, $string, $matches);
// Remove the first element from the results (it's the entire string, we don't want that)
array_shift($matches);
// Join all the others matches with "-"
$formatted = implode('-', $matches);
// And there you have your formatted string
var_dump($string, $formatted);
// Output
// '22223333333334444333' (length=20)
// '22-22-333-333-333-4444-333' (length=26)
报告相同问题?