Domanda

I am trying to get a return value for the day that is matched by the preg_match() function against an associative array to return the selected weekday. The code excerpt is below:

$daysArray = array(
    "Monday" => "Mon", 
    "Tuesday" => "Tues", 
    "Wednesday" => "Wed", 
    "Thursday" => "Thurs", 
    "Friday" => "Fri", 
    "Saturday" => "Sat", 
    "Sunday" => "Sun"
    );
$weekdaysString = implode('|',$daysArray);

if (preg_match('/^\d{1,2}.\d{2}([ap])m\[(' . $weekdaysString . '|-).Only\]$/', $val)) {
    echo "Match was found <br />";
} else if (preg_match('/^\d{1,2}.\d{2}([ap])m\[(' . $weekdaysString . '|-)-(' . $weekdaysString . '|-).Only\]$/', $val)) {
    echo "Match was found 3<br />";
} else {
    echo 'Not found ';
}

$val is '8:55pm[Tues-Thurs Only]';

È stato utile?

Soluzione

This code gives the last day name. This means that if $val contains only one day, this day will be displayed. You can easily change this behavior to get only a result when $val contains two days by removing the ?. If you want to deal with more days, replace ? with *.

I think it is more useful to flip the associative array, to get the day name at the end and to use array_keys to build the day subpattern ($dayPat).

$val = '8:55pm[Tues-Thurs Only]';

$daysCorr = array(
    'Mon'   => 'Monday',
    'Tues'  => 'Tuesday',
    'Wed'   => 'Wednesday',
    'Thurs' => 'Thursday',
    'Fri'   => 'Friday',
    'Sat'   => 'Saturday',
    'Sun'   => 'Sunday');

$dayPat = implode('|', array_keys($daysCorr));

$pattern = <<<EOD
~
^
  \d{1,2} : \d{2} [ap]m
  \[
  (?:
      (?: $dayPat )     # first day  
      (?: - | [ ]&[ ] ) # day delimiter
  )?                    # ? makes the first day optional, remove it if not needed
  (?<day> $dayPat )
  [ ]                   # Since the x modifier is used (free space mode),
                        # you must use [ ] to write a literal space 
  Only ]
\z                      # end of the string
~x
EOD;

if (preg_match($pattern, $val, $match))
    echo $daysCorr[$match['day']];

Altri suggerimenti

Php has functions for that :

echo "Tomorrow is ".date("l", mktime(0, 0, 0, 4, 24, 2014)); 
//=> Tomorrow is Thursday

If what you want is to check if a string contains a weekday you can do this :

$daysArray = array(
    "Monday" => "Mon", 
    "Tuesday" => "Tues", 
    "Wednesday" => "Wed", 
    "Thursday" => "Thurs", 
    "Friday" => "Fri", 
    "Saturday" => "Sat", 
    "Sunday" => "Sun"
    );

$val = "The sun is warm!";

foreach($daysArray as $fullDay => $shortDay){
   if(strpos(strtolower($val), strtolower($shortDay)){
       echo "match on $fullDay!";
   }
}

If you'd take the time to read the documentation on preg_match and preg_match_all you'll find that it is able to find matches and not only returns a boolean value.

Using this new knowledge, you can use the matches to find your weekdays in the string you evaluated.

Try this:

$matches = array();
if (preg_match_all('/^\d{1,2}.\d{2}([ap])m\[(' . $weekdaysString . '|-).Only\]$/', $val, $matches)) {
    echo "Match was found <br />";
    // check the array $matches here
} //...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top