0

I need some help with strpos().

Need to build a way to match any URL that contains /apple-touch but also need to keep specifics matching, such as "/favicon.gif" etc

At the moment, the matches are listed out individually as part of an array:

<?php 

$errorurl = $_SERVER['REQUEST_URI'];
$blacklist = array("/favicon.gif", "/favicon.png", "/apple-touch-icon-precomposed.png", "/apple-touch-icon.png", "/apple-touch-icon-72x72-precomposed.png", "/apple-touch-icon-72x72.png", "/apple-touch-icon-114x114-precomposed.png", "/apple-touch-icon-114x114.png", "/apple-touch-icon-57x57-precomposed.png", "/apple-touch-icon-57x57.png", "/crossdomain.xml");

 if (in_array($errorurl, $blacklist)) { // do nothing }
    else { // send an email about error }

?>

Any ideas?

Many thanks for help

0

2 Answers 2

1

Instead of a regex, you could also remove all occurrences of your blacklist items with str_replace and compare the new string to the old one:

if ( str_replace($blacklist, '', $errorurl) !== $errorurl )
{
  // do nothing
}
else
{
  // send an email about error
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use regex for this, and you want a single regex string that will capture all the values in your existing blacklist plus match any apple-touch string, then something like this would do it.

if(preg_match('/^\/(favicon|crossdomain|apple-touch.*)\.(gif|png|xml)$/',$_SERVER['REQUEST_URI']) {
    //matched the blacklist!
}

To be honest, though, that's far more complex than you need.

I'd say you'd be better off keeping the specific values like favicon.gif etc in the blacklist array you already have; it'd make it a lot easier when you come to adding more items to the list.

I'd only consider using regex for the apple-touch values, since you want to block any variant of them. But even with that, it would likely be simpler if you used strpos().

2 Comments

Thanks for your suggestion. How to use strpos() then?
in fact, I meant substr() :) -- something like if(substr($uri,0,11)=='apple-touch') {....}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.