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 Have the following code, it works with single inputs but fails at compound outputs. The print array function confirms that the array values are correct. I don't know where the problem is.

Single Input:

1:10pm[Sat & Sun Only]

Compound Input:

10:35am, 12:40pm, 1:10pm[Sat & Sun Only]

Code excerpt is below:

function function1($input)
{
    $key1 = ', ';
    if (strpos($input, $key1) !== false) {
        $commas    = substr_count($input, $key1);
        $arraySize = $commas + 1;
        $result1   = explode($key1, $input);
    } else {
        $result1[0] = $input;
    }
    foreach ($result1 as $val) {
        if (preg_match('/^\d{1,2}.\d{2}am\[(?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun).Only\]$/', $input)) {
            echo "Match was found <br />";
        } else if (preg_match('/^\d{1,2}.\d{2}pm\[(?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun).Only\]$/', $input)) {
            echo "Match was found 2<br />";
        } else if (preg_match('/^\d{1,2}.\d{2}am\[(?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun)-(?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun).Only\]$/', $input)) {
            echo "Match was found 3<br />";
        } else if (preg_match('/^\d{1,2}.\d{2}pm\[(?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun)-(?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun).Only\]$/', $input)) {
            echo "Match was found 4<br />";
        } else if (preg_match('/^\d{1,2}.\d{2}am\[(?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun) & (?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun).Only\]$/', $input)) {
            echo "Match was found 5<br />";
        } else if (preg_match('/^\d{1,2}.\d{2}pm\[(?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun) & (?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun).Only\]$/', $input)) {
            echo "Match was found 6<br />";
        } else {
            echo 'Not found ';
        }
        //print $val;
    }
    print_r($result1);
}
$imran = '10:35am, 12:40pm, 1:10pm[Sat & Sun Only]';
function1($imran);
share|improve this question
    
What is your expected output? I think your code can be optimized to just use one regexp. Also function1 is a terrible name for a function. If you can't think of a good name for a function then that function is likely doing something very strange, or too many things. –  Halcyon Apr 22 at 16:29
    
What exactly are you trying to achieve with this code? There's probably a better way to do this. –  Amal Murali Apr 22 at 16:30

1 Answer 1

up vote 2 down vote accepted

You're not checking the values in the array with the regular expressions. Here's what you have:

foreach($result1 as $val){
    if (preg_match('/^\d{1,2}.\d{2}am\[(?:Mon|Tues|Wed|Thurs|Fri|Sat|Sun).Only\]$/', $input)){
...

Notice that you're using $input in your call to preg_match. I think that should be $val.

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.