开发者

Replace URL variable name abbreviation with full name in PHP

开发者 https://www.devze.com 2023-03-05 13:46 出处:网络
I\'m passing a URL variable to a PHP page that is outputting an XML feed of our athletics\' scores. For example, the URL variable for our men\'s basketball XML feed is \"m-baskbl\". Other sports have

I'm passing a URL variable to a PHP page that is outputting an XML feed of our athletics' scores.

For example, the URL variable for our men's basketball XML feed is "m-baskbl". Other sports have similar abbreviations.

The problem is I need to be able to output the full sport name, i.e. "Men's Basketball", but this string doesn't appear in the XML.

My question is, do you know of a way to replace the URL variable abbreviation with the sport name in PHP? For example, have "m-baskbl" output "Men's Basketball".

Here is my page, XML and code:

        <?php
        // Output the current school year (previous year through July 31, current year on/after August 1)
        $year = date('Y') - 1;
        $month = date('n');
        if($month >= 8) $year++;

        // Build the XML file path
        $p ="http://www.gostanford.com/data/xml/events/";
        $s = $_GET['s'];
        $e = "/$year/index.xml";
        $file = "$p$s$e"; 

        $xml = simplexml_load_file($file);

        // Reverse chronological order
        $xmlArray = array();
        foreach ($xml as $event_date) $xmlArray[] = $event_date;
        $xmlArray = array_reverse($xmlArray);

        foreach ($xmlArray as $event_date)
        {
            // Only output if complete
            if(!empty($event_date->event['vn']) && !empty($event_date->event['hn']) && !empty($event_date->event['vs']) && !empty($event_date->event['hs']))
            {
            // Is Stanford the home team? 
            $home = ($event_date->event['hc'] == 'stan');
            // Only show opponents' name
            $name = $home ? $event_date->event['vn'] : $event_date->event['hn'];
  开发者_高级运维          // Output "vs" if home game or "at" if away game
            $preposition = $home ? 'vs' : 'at';
            // Tie?
            if((int)$event_date->event['hs'] == (int)$event_date->event['vs'])
            {
            $result = 'Tie'; // Tie
            }
            else if((int)$event_date->event['hs'] > (int)$event_date->event['vs']) // Home team won? 
            {
            if($home) // Home?
            {
            $result = 'Win'; // You're home and the home team won
            }
            else
            {
            $result = 'Loss'; // You're away, but the home team won
            }
            }
            else // Home team lost
            {
            if($home) // Home?
            {
            $result = 'Loss'; // You're away, but the home team won
            }
            else
            {
            $result = 'Win'; // You're home and the home team won
            }
            }
            echo '<li>';
                echo '<h3>', $preposition, ' ', $name, ' - ', '<em>', $result, '</em></h3>';
                echo '<p><strong>', $event_date->event['vn'], ' ', $event_date->event['vs'], ' - ', $event_date->event['hn'], ' ', $event_date->event['hs'], '</strong></p>';
                echo '<p>', date('F j, Y', strtotime($event_date['date'])), '</p>';
            echo '</li>';
            }
        }   
        ?>


You can probably do a simple array mapping e.g.

$Abbreviations = array(
   'm-baskbl' => "Men's Basketball",
   'w-baskbl' => "Women's Basketball"
);

// somewhere you know you're fetching data for "m-baskbl"
// use that variable to fetch the actual name
$Division = $Abbreviations['m-baskbl']; // Men's Basketball

Just an idea :)

0

精彩评论

暂无评论...
验证码 换一张
取 消