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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看