tadakan Posted June 20, 2011 Share Posted June 20, 2011 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 https://forums.phpfreaks.com/topic/239839-regex-city-name-from-string-and-then-compare-to-text-file/ Share on other sites More sharing options...
fugix Posted June 20, 2011 Share Posted June 20, 2011 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 https://forums.phpfreaks.com/topic/239839-regex-city-name-from-string-and-then-compare-to-text-file/#findComment-1232040 Share on other sites More sharing options...
tadakan Posted June 20, 2011 Author Share Posted June 20, 2011 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 https://forums.phpfreaks.com/topic/239839-regex-city-name-from-string-and-then-compare-to-text-file/#findComment-1232064 Share on other sites More sharing options...
tadakan Posted June 20, 2011 Author Share Posted June 20, 2011 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 https://forums.phpfreaks.com/topic/239839-regex-city-name-from-string-and-then-compare-to-text-file/#findComment-1232066 Share on other sites More sharing options...
fugix Posted June 20, 2011 Share Posted June 20, 2011 Okay I see your issue, you are telling your regex to remember two strings instead of just the one. Try this. $reg_exCity = '/^Locations: (\w+\s?\w+)$/i'; Link to comment https://forums.phpfreaks.com/topic/239839-regex-city-name-from-string-and-then-compare-to-text-file/#findComment-1232266 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.