neridaj Posted October 16, 2008 Share Posted October 16, 2008 Hello, I'm using a simple regex to validate property address so that they only contain alphanumeric and underscore characters. However, when I test the script on regexlib.com it works as expected, but when I run it from my script it does not. The script I'm using takes a property address (pa) from the query string and runs it through the regex for true or false. The validation script passes true for "120_Ho_St" but false for "120_Howe_St"? In fact, any instance of "Howe" in the query string returns false. It seems simple enough but I just don't understand, if you do I would appreciate any input. function valid_propadd($pa) { if (ereg('(\w(\s)?)+', $pa)) return true; else return false; } $pa = $_GET['pa']; if(!valid_propadd($pa)) die("invalid property address!"); else $propadd = $pa; Thanks, Jason Link to comment https://forums.phpfreaks.com/topic/128764-simple-regex-headaches/ Share on other sites More sharing options...
DarkWater Posted October 16, 2008 Share Posted October 16, 2008 Don't use ereg() or any functions in the POSIX library. Try: function valid_propadd($pa) { if (preg_match('/^\w+$/', $pa)) return true; else return false; } Link to comment https://forums.phpfreaks.com/topic/128764-simple-regex-headaches/#findComment-667413 Share on other sites More sharing options...
effigy Posted October 16, 2008 Share Posted October 16, 2008 Shorthands are not available in EREG; use PREG. Also, why the \s? <pre> <?php function valid_propadd($pa) { return preg_match('/(?:\w\s?)+/', $pa); } $tests = array( '120_Ho_St', '120_Howe_St', ); foreach ($tests as $test) { echo $test, ' => ', valid_propadd($test) ? 'Valid' : 'Invalid' ; echo '<br>'; } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/128764-simple-regex-headaches/#findComment-667420 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.