在PHP中使用全名替换URL变量名称缩写

时间:2011-05-13 18:19:45

标签: php xml replace

我正在将一个URL变量传递给PHP页面,该页面输出了我们田径运动成绩的XML Feed。

例如,men's basketball XML feed的网址变量是“m-baskbl”。其他运动也有类似的缩写。

问题是我需要能够输出完整的运动名称,即“男子篮球”,但这个字符串不会出现在XML中。

我的问题是,你知道如何用PHP中的运动名称替换URL变量缩写吗?例如,将“m-baskbl”输出为“男子篮球”。

以下是我的pageXML和代码:

        <?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>';
            }
        }   
        ?>

1 个答案:

答案 0 :(得分:1)

你可以做一个简单的数组映射,例如

$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

只是一个想法:)