-3

我正在尝试从数据库中获取事件名称(例如:Manny Pacquiao vs. Juan Manuel Marquez),将事件名称拆分为 2 个名称(Manny Pacquiao、Juan Manuel Marquez)并在页面上使用它们。

这是我的代码不起作用。

    if(count($perfarray) == 2 && $page['catnum'] == "1142")
    {

        $input = $event['Event'];
        $params = explode('vs.', $input);
        foreach($params as $param) 
            {
                $kv = explode('vs.', $param);
                $fighterNo1 = $kv[0];
                $fighterNo2 = $kv[1];
            }

        $fulltext = str_replace("\$Fighter1", $fighterNo1, $fulltext);
        $fulltext = str_replace("\$Fighter2", $fighterNo2, $fulltext);

        // $fulltext = str_replace("\$Fighter1", addslashes($perfarray[0]), $fulltext);
        //$fulltext = str_replace("\$Fighter2", addslashes($perfarray[1]), $fulltext);
    }
    elseif(strstr($event['Event']," vs."))
    {
        $input = $event['Event'];
        $params = explode('vs.', $input);
        foreach($params as $param) 
            {
                $kv = explode('vs.', $param);
                $fighterNo1 = $kv[0];
                $fighterNo2 = $kv[1];
            }

        $fulltext = str_replace("\$Fighter1", $fighterNo1, $fulltext);
        $fulltext = str_replace("\$Fighter2", $fighterNo2, $fulltext);
    }
4

3 回答 3

1
    $input = $event['Event'];
    $params = explode('vs.', $input);
    foreach($params as $param) 
        {
            $kv = explode('vs.', $param);
            $fighterNo1 = $kv[0];
            $fighterNo2 = $kv[1];
        }

在上面的第二行中,您已经正确explode$params吗?在第 5 行,你又explodeing 了?

于 2012-09-17T17:11:24.247 回答
1

你叫了explode太多次了。

$params = explode('vs.', $input);
foreach($params as $param) {
   $kv = explode('vs.', $param);

您正在获取第一个参数,然后explode再次尝试。这应该有效,我认为:

$input = $event['Event'];
$params = explode('vs.', $input);
$fighterNo1 = $params[0];
$fighterNo2 = $params[1];

$fulltext = str_replace("\$Fighter1", $fighterNo1, $fulltext);
$fulltext = str_replace("\$Fighter2", $fighterNo2, $fulltext);
于 2012-09-17T17:12:08.123 回答
0

我不确定您的数据到底是什么样子,但这是您可以做的

$text = "Manny Pacquiao vs. Juan Manuel Marquez";

list($fighter1,$fighter2) =explode(" vs. ",$text);

$fulltext = str_replace("\$Fighter1", $fighter1, $fulltext);
$fulltext = str_replace("\$Fighter2", $fighter2, $fulltext);
于 2012-09-17T17:12:54.813 回答