Jump to content

Simple regex headaches


neridaj

Recommended Posts

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

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.