0

Basically I'm checking days of the week from an array to a string based on an initialization of the day for instance:

<?php
$check_days = array("M", "T");
$days1 = "MTW";
$days2 = "M";
[insert code to compare $check_days to $days1 and $days2 so that $days1 returns FALSE while $days2 returns TRUE]
?>
1
  • Sorry guys, something has come up and I have to tend to it immediately. I'll try your solutions and mark the appropriate solution when I return. Thanks. Commented Sep 27, 2014 at 0:55

3 Answers 3

3

Try using a regex character class and implode:

if (preg_match('/^['.implode($check_days).']+$/', $days1)) {
    // do some stuff
}

EDIT: Let me help explain what's going on here:

implode($check_days)

This combines all the elements of an array into a single string. In your case, this is "MT".

preg_match('/^[MT]+$/', $days1);

This is a regular expression that checks that after the 'beginning' (^), $days1 contains either an "M" or a "T" ([MT]), repeated one or more times (+), then the string ends ($). It returns true if this is the case.

Hope that helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply. I haven't ignored you. I'm just trying to wrap my head around whats going on here. reading documentation.
No. Matches 'MMMMMWWWWTTTT', as well.
@PHPglue I'd be happy to amend the answer if OP further requests that characters not repeat. As it stands, I just provide an example of "return false if it contains any characters not in the array"
I'm really sorry to get back so late (life happened) but this is just what I needed. Repeating will not happen because the variables are coming from a database with a standardized format. Thanks for teaching me a new function!
@GarrettBlackmon It's really no problem at all :) many people ask questions and forget about them altogether.
0

Sorry, I read your question wrong, and so my answer is wrong too. I'd go with wavemode's solution.

Original answer

If I understand your requirements correctly, you want to make sure all the days in check_days must exist in the variable to be tested. If I'm correct, this function will work:

function checkDays($haystack, $validator) {
    $ok = true;
    foreach ($validator as $day) {
        $ok = (strpos($haystack, $day) !== false) && $ok;
    }
    return $ok;
}

See it in action in this phiddle: http://phiddle.net/6

Comments

0

Try this:

function checkDays($testFor, $inString){
  $a = str_split($inString); $d = array();
  foreach($a as $v){
    if(!in_array($testFor, $v) || in_array($d, $v)){
      return false;
    }
    $d[] = $v;
  }
  return true;
}
$check_days = array('M', 'T');
echo checkDays($check_days, 'MTW');
echo checkDays($checkdays, 'M');

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.