duanbaque4230 2017-01-05 23:45
浏览 38
已采纳

XML:appendChild()有两个循环? (PHP)

I have a problem... I want to know if the URL or TITLE in my XML file is containing some specified keywords. If yes, they should be added to the specified product. If no, a "Nothing" should be added to the specified product.

But my problem is that I don't have the knowledge to programm it so, that the program checks if the keywords from the array are in the URL or TITLE. Because if not the first keyword of the array is in the URL or TITLE it will always go into the else tribe and add "Nothing"... How can I say that the progamm is only allowed to add "Nothing" if the program checked the 7 keywords in the $searches array?

Also the program must consider that the keyword values above 3 should be saved as "PlayStation" and not as "PS3", "PS4" or "PSN"!

And in addtion, how can I add them to my prodcuts? - I don't think that my code below is doing that...

Code:

$searches = ["Steam", "Uplay", "Xbox", "Nintendo", "PS3", "PS4", "PSN"];

function isContaining($searches, $titleTag, $urlTag, $productTag, $path){

    $dom = new DOMDocument('1.0', 'utf-8');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->load($path);
    $root = $dom->documentElement;

    $markerTitle = $root->getElementsByTagName($titleTag);
    $markerURL = $root->getElementsByTagName($urlTag);

    $plat = array();

    for($i = $markerTitle->length - 1; $i >= 0 ; $i--){

        $title = $markerTitle->item($i)->textContent;
        $url = $markerURL->item($i)->textContent;

        $co = count($searches);

        for($j = 0; $j < $co; $j++){
            if(stripos($title, $searches[$j]) !== false){
                if($j > 4){
                    array_push($plat, "PlayStation");
                }elseif($j < 5){
                    array_push($plat, $searches[$j]);
                }
            }elseif(stripos($url, $searches[$j]) !== false){
                if($j > 4){
                    array_push($plat, "PlayStation");
                }elseif($j < 5){
                    array_push($plat, $searches[$j]);
                }
            }elseif($j == 7 && stripos($url, $searches[$j]) == false){
                array_push($plat, "Nothing");
            }
        }
    }

    array_reverse($plat);
    print_r($plat);
    $c = count($plat);

    for($i = 0; $i < $c; $i++){
        $node = $dom->createElement('plat', $plat[$c]);
        $dom->getElementsByTagName($productTag)->item($i)->appendChild($node);
    }


    $dom->saveXML();
    $dom->save($path);
}

isContaining($searches, "name", "link", "product", $gamesplanetPath);

Greetings and Thank you!

  • 写回答

1条回答 默认 最新

  • doutan1637 2017-01-06 03:54
    关注

    You can try to replace your inner for loop with this following code:

    $productFound = false;
    for($j = 0; $j < $co; $j++){
        if(stripos($title, $searches[$j]) !== false){
            if($j > 3){
                array_push($plat, "PlayStation");
            }else{
                array_push($plat, $searches[$j]);
            }
            $productFound = true;
        }elseif(stripos($url, $searches[$j]) !== false){
            if($j > 3){
                array_push($plat, "PlayStation");
            }else{
                array_push($plat, $searches[$j]);
            }
            $productFound = true;
        }
    }
    if($productFound == false){
        array_push($plat, "Nothing");
    }
    

    NOTE: the array index start from 0

    Regarding your last question

    how can I add them to my prodcuts?

    I don't know what you mean by adding to your product.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么