Parsing this string with regex PHP

Sahil

I have string in format as below:

ABC 318 XY2388

I tried to use preg replace function in PHP with Regex but having no knowledge of reg ex I am not getting anywhere.

I want above string to end up like ABC 318/XY 2388

So the rule is:

  • Keep the first ABC (first batch of letters) untouched.
  • Put a space between letter(s) and number occurring after ABC.
  • replace space between second batch of numbers and letters with forward slash

I will appreciate any help on this.

Racso

This code changes the string using the corrected rule/example you are giving:

  $input = "ABC 318 XY2388";
  $output = preg_replace("/([A-Z]*) (\d*) ([A-Z]*)(\d*)/","$1 $2/$3 $4", $input);
  echo $output;

Result: ABC 318/XY 2388

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related