0

I have an array of urls

urls = [ 'example.co.uk/nz', 'example.com/us', 'exclude.example.co.uk' ];

now I wanted to do some manipulations with the above 2 urls and want to skip the third. here is my code.

function xyz($url) {
     $url_locale = preg_match( '#(.co.uk|.com)/([a-zA-Z]{2})/#', $url, $matches );
     
      if ( ! empty( $matches[2] ) ) {
         // do something
       } else {
         // do something else
      }

   }

xyz( $url[0] ); //do something
xyz( $url[1] ); //do something
xyz( $url[2] ); //do something else

so basically it should exclude the exclude.example.co.uk from the preg_match
New contributor
Ashish Rawat is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
6
  • 1
    Can't you just remove xyz( $url[2] );? Jun 22 at 7:34
  • 2
    Welcome to wpse! This seems like a generic php question and not at all wordpress related, it would better fit in SO. Jun 22 at 7:35
  • @JacobPeattie that's just a mock data, we have no idea if we have it in the array or not. just in case it's there it should be handled that way. the array may contain n number of urls so ... Jun 22 at 7:37
  • @Buttered_Toast thanks, I will keep that in mind. Jun 22 at 7:37
  • By what criteria do you want to exclude items? Jun 22 at 7:46

0

Browse other questions tagged or ask your own question.