1

I've searched til I am blue in the face and have yet to find a solution to this. I want to check a string to see if one of the 2 letter state abbreviations are in it. I can find it and uppercase it, but it also uppercase the abbreviation if it is part of a word (which I do not want it to do).

My code below outputs "WAnt To Work In Eastern WA?"

when I want it to output "Want To Work In Eastern WA?".

Here is what I am working with, hopefully someone can tell me what I am doing wrong. Thank you!

$newheading = "Want To Work In Eastern Wa?";  // This variable is not always the same.  
$states = "Al Ak Az Ar Ca Co Ct De Fl Ga Hi Id Il In Ia Ks Ky La Me Md Ma Mi Mn Ms Mo Mt Ne Nv Nh Nj Nm Ny Nc Nd Oh Ok Or Pa Ri Sc Sd Tn Tx Ut Vt Va Wa Wv Wi Wy";
    $pieces = explode(" ", trim($newheading));
    
    foreach($pieces as $v) {
            if ((strlen($v) == 2) && ($v != "In") && ($v != "Or")) {
                $newv = strtoupper($v);
                $out =  str_replace($v, $newv, $pieces);
                $out = implode(" ", $pieces);
                $newheading = $out;
                break;
            }
    }
1
  • Did you give up? Commented Mar 2, 2021 at 4:24

1 Answer 1

1

You can use a regex joining the states on OR | and look for word-boundaries \b or a space \s:

$states = explode(' ', $states);  // or just define as an array
$states = implode('|', $states);

$newheading = preg_replace_callback("/\b($states)\b/", function($m) {
                                                           return strtoupper($m[0]);
                                                       }, $newheading);

I'm not sure what you're doing with "In" and "Or", but just remove them from $states.

If the string could contain wa or wA for example, then you want to add the i modifier to the end for case insensitive.

Sign up to request clarification or add additional context in comments.

2 Comments

The words in $newheading are not always the same. Does your code account for this? It looks like it just checks the first word. And thank you for replying.
It checks all of the words. preg_replace and preg_replace_callback look for all matches 3v4l.org/HaQ8g I added the i modifier to catch ca and tx in my example.

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.