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
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>

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.