[nycphp-talk] REGEXP Solution Needed
Scott Mattocks
scott at crisscott.com
Wed Sep 8 09:19:15 EDT 2010
On 09/08/2010 08:30 AM, ps at blu-studio.com wrote:
> Using GNU Regular Expressions I need to examine an URL like those below,
> checking the size key and value, I need to capture and block all URLs
> where 'size does not equal 10'. In other words "size=12", not
> acceptable.
Regular expressions are expensive and should only be used when
absolutely necessary. If you are checking for a specific string, just
check for it with str* functions. Here's how I would check for it:
$key = 'size';
$val = 10;
$url = 'http://....';
$last = strrpos($url, $key . '=');
if ($last !== false && $last == strrpos($url, $key . '=' . $value))
{
echo 'Good';
}
else
{
echo 'Bad';
}
That block of code makes sure that 'size=' shows up in your URL and that
the last occurrence of 'size=' is actually 'size=10'. The last
occurrence is the value that will be passed to the server so that's
probably the only one you care about. If you want to verify that there
is only one occurrence use strpos(...) == strrpos(...) in addition to
the checks above.
--
Scott Mattocks
More information about the talk
mailing list