Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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]';

share|improve this question
    
As I can't see a question in your... post, I'd take a wild guess and suggest you using the third argument for preg_match –  Havelock Apr 23 at 8:47
    
@Havelock there are only two arguments, and it's the second one –  difyzz Apr 23 at 8:52
    
Since when is Except in your Pattern? Your regex doesn't match that. stackoverflow.com/questions/23221936/… –  Andresch Serj Apr 23 at 8:58
    
@AndreschSerj edited –  difyzz Apr 23 at 9:01

3 Answers 3

up vote 0 down vote accepted

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']];
share|improve this answer
    
how do I return the time part of the code? That is, the \d{1,2} : \d{2} [ap]m part –  difyzz Apr 24 at 8:29

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!";
   }
}
share|improve this answer
    
the variable $weekdays is an array of weekdays that is checked by preg_match for a match int he pattern that it is included in. I want it to return the day that it matched. For example, if in the array of weekdays, the pattern matched 'Wed', I want it to return Wednesday. –  difyzz Apr 23 at 8:58
    
I think I get it, I've edited my answer. –  Loïc Apr 23 at 9:06

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
} //...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.