duanbaque4230 2017-01-05 15: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-05 19: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 pycharm倒入虚拟环境的时候,显示这个,但是我的虚拟环境已经创建了
  • ¥15 FPGA芯片60进制计数器
  • ¥15 前端js怎么实现word的.doc后缀文件在线预览
  • ¥20 macmin m 4连接iPad
  • ¥15 DBIF_REPO_SQL_ERROR
  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部