0

I have any array of data "example.com/imports", "example.com/var", "example.com/js" i want to remove all urls which contain this for sitemap.

Some of my url data is like the following

"example.com/imports/product.html",
"example.com/imports/product1.html",
"example.com/var/cache/5t46fdgdyg7644gfgfdgr",
"example.com/js/scripts.js"

I have this code

for ($i = 0; $i <= count($urls); $i++) {

$url = $urls[$i];

if (in_array($url, $remove_urls)) {
// found remove url
}else{
echo $url;
}
}

However this only removes if the url is exact match such as "example.com/imports" is there a way to check against start

1
  • 1
    what you want to remove? Commented Nov 18, 2014 at 9:21

2 Answers 2

4

Instead of in_array($url, $remove_urls) try to use strpos:

foreach ($urls as $url) {
  $remove = false;

  // loop $remove_urls and check if $url starts with any of them
  foreach ($remove_urls as $remove_url) {
    if (strpos($url, $remove_url) === 0) {
      $remove = true;
      break;
    }
  }

  if ($remove) {
    // remove url
  } else {
    echo $url;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Works great just had change $remove_urls to $remove_url in strpos
Maybe I'm being an efficiency nerd here, but I would prefer if( substr($url,0,strlen($remove_url)) === $remove_url), because strpos will check all positions of the string before failing whereas this only checks the first - the one we're interested in XD
0

You can use preg_grep function like that:

$urls = ['imports', 'var', 'js'];
$url_pattern = '/example.com\/(' . implode('|', $urls) . ')\/.*/';
$removed = preg_grep($url_pattern, $remove_urls);

here an example.

Comments

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.