0

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"
6
  • Post the actual code where you want to use this because as it stands, this question isn't very clear about what you're trying to accomplish Commented Jun 20, 2018 at 14:43
  • @WillardSolutions - added a bit more clarification as to the expected input and output. Commented Jun 20, 2018 at 14:45
  • 1
    Why are you looking for something else than sprintf? Commented Jun 20, 2018 at 14:51
  • 1
    Sounds like an XY problem. A regex of [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. Commented Jun 20, 2018 at 14:57
  • @NicoHaase - does sprintf format characters? (see last example) Commented Jun 20, 2018 at 15:06

1 Answer 1

2

Use preg_replace with preg_match check before applying it. Example:

function formatMyString($string) {
    $patterns = [
        [
            'name' => 'identity',
            'regex' => '^([0-9]{3})([0-9]{3})([0-9]{4})$',
            'format' => '$1 $2 $3'
        ]
    ];

    foreach ($patterns as $pattern) {
        if (preg_match('/'.$pattern['regex'].'/', $string)) {
            return preg_replace('/'.$pattern['regex'].'/', $pattern['format'], $string);
        }
    }

    return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Have used a slightly altered version of this - added a !== false check for the preg_match call, and just used the bit inside the foreach loop ... but it works perfectly. Thanks :)
@AlgyTaylor, preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred. If you only use !== false it will let non-matches slip through and will break the logic.

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.