I have a set of strings that I'd like to display in a specific format:
| Type | Example | Regex | Output |
|----------|------------|-------------------------|----------------|
| Ref | 0909090 | [0-9]{8} | "09090909" |
| Identity | 6001002003 | [0-9]{10} | "600 100 2003" |
| Internal | M12/45678 | [Mm][0-9]{2}/[0-9]{4,8} | "M12 / 45678" |
Is there a function in PHP that would allow me to pass something like a regular expression or a sprintf string that would allow me to format a string in a particular way.
It doesn't need to be either of those, but it does need to be able to be specified as a string. This is so I can store it in some kind of data object that will look like:
[
{
name: "identity",
regex: "[0-9]{10}",
format: "%3c %3c %4c" /* or whatever it ends up being */
},
// ....
]
The function should work along the lines of:
echo formatMyString('6001002003', '%3c %3c %4c') // returns "600 100 2003"
sprintf?[0-9]{10}doesn't really make sense if you need three parts. You'd probably be better off writing functions to handle each format rather than doing something dynamic like this.sprintfformat characters? (see last example)