How can I write this in a shorter way?

Posted by oaziz on Stack Overflow See other posts from Stack Overflow or by oaziz
Published on 2011-11-22T01:39:15Z Indexed on 2011/11/22 1:50 UTC
Read the original article Hit count: 98

Filed under:

This code repeats itself. How can I make it shorter? Maybe by using anonymous function or something?

foreach ($value as $wrong) {
  if (starts_with($wrong, '%') and ends_with($wrong, '%')) {
    $wrong = trim($wrong, '%');
    if (contains($address, $wrong)) {
      $corrected_address = str_replace($wrong, $key, $address);
      break;
    }
  }
  else {
    $wrong = trim($wrong, '%');
    if (ends_with($address, $wrong)) {
      $corrected_address = str_replace($wrong, $key, $address);
      break;
    }
  }
}

Thanks.

© Stack Overflow or respective owner

Related posts about php