I have a pretty large database with some data listed in this format, mixed up with another bunch of words in the keywords column.
BA 093, RJ 342, ES 324, etc.
The characters themselves always vary but the structure remains the same. I would like to change all of the strings that obey this character structure : 2 characters A-Z, space, 3 characters 0-9 to the following:
BA-093, RJ-342, ES-324, etc.
Be mindful that these strings are mixed up with a bunch of other strings, so I need to isolate them before replacing the empty space. Here is a sample string:
Km 111 aracoiaba Araçoiaba sp 270 spvias vias sao paulo Araçoiaba Bidirecional
sp 270 is the bit we want to change.
EDIT: There was also an exception which should ignore the condition in case KM are the first two characters, it was handled by one of the answers
I have written the beginning of the script that picks up all the data and shows it on the browser to find a solution, but I'm unsure on what to do with my if statement to isolate the strings and replace them. And since I'm using explode it is probably turning the data above into two separate arrays each, which further complicates things.
<?php
require 'includes/connect.php';
$pullkeywords = $db->query("SELECT keywords FROM main");
while ($result = $pullkeywords->fetch_object()) {
$separatekeywords = explode(" ", $result->keywords);
print_r ($separatekeywords);
echo "<br />";
}
Any help is appreciated. Thank you in advance.
$result->keywords?BA 093, RJ 342, ES 324or justBA 093???Km 111 aracoiaba Araçoiaba sp 270 spvias vias sao paulo Araçoiaba Bidirecionalis an example of the string I'm working with.