0

I have data in the format:

Football - 101 Carolina Panthers +15 -110 for Game

Football - 101 Carolina Panthers/Pittsburgh Steelers under 36½ -110 for Game

Football - 102 Pittsburgh Steelers -9 -120 for 1st Half


How to transform this into a PHP array:

$game_data[] = array( 'sport_type'  => 'Football',
                      'game_number' => 101,
                      'game_name'   => 'Carolina Panthers',
                      'runline_odd' => '+15 -110',
                      'total_odd'   => '',
                      'odd_type'    => 'runline',
                      'period'      => 'Game' );

$game_data[] = array( 'sport_type'  => 'Football',
                      'game_number' => 101,
                      'game_name'   => 'Carolina Panthers/Pittsburgh Steelers',
                      'runline_odd' => '',
                      'total_odd'   => 'under 36½ -110',
                      'odd_type'    => 'total_odd',
                      'period'      => 'Game' );

$game_data[] = array( 'sport_type'  => 'Football',
                      'game_number' => 102,
                      'game_name'   => 'Pittsburgh Steelers',
                      'runline_odd' => '-9 -120',
                      'total_odd'   => '',
                      'odd_type'    => 'runline',
                      'period'      => '1st Half' );
3
  • 9
    What have you tried so far? (Your question reads as if you're asking for a ready solution, but have zero intention to learn and/or understand regular expressions.) Commented Dec 23, 2010 at 9:31
  • 2
    Welcome to StackOverflow! In the future, if you want to display large chunks of code, you can prepend each line with four spaces, as I reformatted your question to do. Commented Dec 23, 2010 at 9:49
  • @Antal S-Z: Thank You :3 Commented Dec 23, 2010 at 10:11

2 Answers 2

1

Normally I wouldn't solve the whole problem for someone, but the ½ character made it interesting enough. Now, I'm not a super expert on regexes so this might not be the most optimized or elegant solution, but it seems to get the job done. At least with the provided sample input.

EDIT: Oops. Didn't catch that under was actually part of the runline_odd data. So this does actually not currently get the job done. I'll be back.

EDIT2: Revised the regex slightly and it now correctly matches between runline_odd and runline_total.

<?php
$input = array(
'Football - 101 Carolina Panthers +15 -110 for Game',
'Football - 101 Carolina Panthers/Pittsburgh Steelers under 36½ -110 for Game',
'Football - 102 Pittsburgh Steelers -9 -120 for 1st Half'
);

$regex = '^(?<sport_type>[[:alpha:]]*) - '.
         '(?<game_number>[0-9]*) '.
         '('.
            '(?<game_nameb>[[:alpha:]\/ ]*?) '.
            '(?<runline_total>(under ([0-9\x{00BD}]+){1}) ((-|\+)?([-+0-9\x{00BD}]+){1})) for '.
         '|'.
            '(?<game_namea>[[:alpha:]\/ ]*) '.
            '(?<runline_odd>((-|\+)?([0-9\x{00BD}]+){1}) ((-|\+)?([-+0-9\x{00BD}]+){1})) for '.
         ')'.
         '(?<period>.*)$';


$game_data = array();

foreach ($input as $in) {
    $matches = false;
    $cnt = preg_match('/' . $regex . '/ui', $in, $matches);

    if ($cnt && is_array($matches) && count($matches)) {
        if (empty($matches['game_nameb'])) {
            $game_name = $matches['game_namea'];
            $runline_odd = $matches['runline_odd'];
            $total_odd = '';
        } else {
            $game_name = $matches['game_nameb'];
            $runline_odd = '';
            $total_odd = $matches['runline_total'];
        }


        $result = array(
            'sport_type' => $matches['sport_type'],
            'game_number' => $matches['game_number'],
            'game_name' => $game_name,
            'runline_odd' => $runline_odd,
            'total_odd' => $total_odd,
            'period' => $matches['period']
        );

        array_push($game_data, $result);
    }
}

var_dump($game_data);

This produces the following:

$ /usr/local/bin/php preg-match.php 
array(3) {
[0]=>
  array(6) {
    ["sport_type"]=>
    string(8) "Football"
    ["game_number"]=>
    string(3) "101"
    ["game_name"]=>
    string(17) "Carolina Panthers"
    ["runline_odd"]=>
    string(8) "+15 -110"
    ["total_odd"]=>
    string(0) ""
    ["period"]=>
    string(4) "Game"
  }
  [1]=>
  array(6) {
    ["sport_type"]=>
    string(8) "Football"
    ["game_number"]=>
    string(3) "101"
    ["game_name"]=>
    string(37) "Carolina Panthers/Pittsburgh Steelers"
    ["runline_odd"]=>
    string(0) ""
    ["total_odd"]=>
    string(15) "under 36½ -110"
    ["period"]=>
    string(4) "Game"
  }
  [2]=>
  array(6) {
    ["sport_type"]=>
    string(8) "Football"
    ["game_number"]=>
    string(3) "102"
    ["game_name"]=>
    string(19) "Pittsburgh Steelers"
    ["runline_odd"]=>
    string(7) "-9 -120"
    ["total_odd"]=>
    string(0) ""
    ["period"]=>
    string(8) "1st Half"
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Sarah: I was aware of that. Took me a while to modify the regex, but now it's working.
It's not matching the 2nd string :) which results in array with 2 elements.
@Sarah: As you can see from my provided sample output, it should indeed work fine, so I suspect the problem lies elsewhere. Can you check: a) Which PHP version are you running it on? b) Which encoding is the data in? -- I'd say it's likely to be an encoding problem. The preg_match with the u flag expects unicode input. Perhaps you need to utf8_encode your input.
@Sarah: brilliant, glad to be of assistance :)
1

Following works except the case where there is an under after gmae name:

/([^-]+)\s*-\s*(\d+)\s*([^\d+-]+)\s*((?:under\s*)?[\d\s+-]+)\s*for\s*(.+)/

Explanation:

([^-]+): Match anything other than -, which is separating gmae name from other details.
\s*-\s*: - surrounded with spaces
(\d+)  : Game number
([^\d+-]+): Anything other than +, -, a digit. Matches gmae name.
((?:under\s*)?[\d\s+-]+): runline odd or total odd.

PS:

  1. Take care of the cases where there is 'under'. The regex above is dumping it with game_name.
  2. Take care of unicode chars.

2 Comments

You can image the user has more then 3 line of inputs and it's not going to work every time...
@danip, true, and i don't even know football to know all possible cases. But based on the three lines (apart from the under case which i mentioned) i do see a pattern: (string) - (number) (string) (number) for (string).

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.