1

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);
2
  • 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. Commented Apr 22, 2014 at 16:29
  • What exactly are you trying to achieve with this code? There's probably a better way to do this. Commented Apr 22, 2014 at 16:30

1 Answer 1

2

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.

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.