Jump to content

Regex city name from string and then compare to text file


tadakan

Recommended Posts

I'm trying to regex a city name from a string (an email that gets passed to the script) and then compare it to a text file so that I can cue an IF statement on whether or not it matches a city in the text file.

 

I've got a regex that I think is correct, but I'm not sure how to pull the city name from the $matches and then load the list of cities and compare it.  I've been messing with array_slice() and explode() and was starting to devolve into layers upon layers of preg_matchs so it seemed like it was time to ask for help.

 

/*Get email contents from the piped email.  I'm just plugging
in the value in the form that it will come from the email
so I don't have to send endless emails to myself to test */
//$email = file_get_contents('php://stdin');
$email = "Locations: King City";

       //Regular expression to find the city name
$reg_exCity = '/Locations: (\w+) (\w+)?/';

//Apply the city regex to the contents of the email
preg_match($reg_exCity, $email, $city);

       //store city names in array
      $citiesArray = explode(",","cities.txt");

 

at this point I'm not even sure what the best way to compare the list to the value from the email will be.  I guess another preg_match?

         //turn the name of the city into a regex
$reg_exCities = '/'.$city[1].'/'.'i';       

        if (preg_match($reg_exCities, $citiesArray, $matches)) {
                 echo "this one!";
               }
        else {
                 echo "not this one";
               }

 

Thanks for any help, it's been a while since I did much scripting, and I was never that great at it so this project is really making my brain stretch.

Link to comment
Share on other sites

Instead of using another preg_match() etc to try and compare your email value to your text file, why not use the function in_array() which compares the value of mixed types of data to a value in an array.

 

        if (in_array($cities[1], $citiesArray) {

                 echo "this one!";

               }

        else {

                 echo "not this one";

               }

Link to comment
Share on other sites

I experimented with in_array() a little bit, but I still have the problem of turning an array with up to two values (ie a city name with two parts) into a single string... OR potentially rewriting my regex so that it will make $match[1] include both halves of the city name.  Thoughts as to which is better or if one will even work?

Link to comment
Share on other sites

Related to that problem, given:

$email = "Locations: King City";
$reg_exCity = '/Locations: (\w+) (\w+)?/';
preg_match($reg_exCity, $email, $city);
echo $city[1];
echo $city[2];

returns KingCity

 

$city1 = array_slice($city,1,2);
echo $city1;

returns Array

 

I think I'm trying to use the correct function, but I'm obviously doing it wrong.  Suggestions?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.