dongshen9686 2013-12-18 15:29
浏览 124
已采纳

如何使用php搜索xml文件中的多个关键字并返回包含标签?

I have an xml file like this, which stores video subtitles:

<videos>
    <video>
        <id>1</id>
        <enSub>Hello Foo! Good morning!</enSub>
        <cnSub>你好 Foo! 早上好!</cnSub>
    </video>
    <video>
        <id>2</id>
        <enSub>Hello Bar! Good afternoon!</enSub>
        <cnSub>你好 Bar! 下午好!</cnSub>
    </video>
</videos>

I want to search certain keywords through this xml, like I enter "hello moning" in the search text area, and the search result could find the video element with id "1".

I guess that using php xpath can only find single keyword in the xml file, and it has to iterate through the whole tree. I'm not confident that I can write a function with good performance.

I tried to use external resource like google custom search to search my web, but it turned out that I'm not using pages to display each video. I pass different video id as parameter to the video-play-page.

I also thought of regular expression, but don't know how to handle the orders of keywords.

So is there any search engine that I can use to search multiple keywords to pinpoint a video. I designed this to help my users to quickly find the video the watched.

I googled a lot. It's really slow, sometimes I just can't access google, in my place in China here. I tried "multiple keywords search xml" as searching keywords. Maybe my English isn't intelligent enough for google to understand my intent. I hope you guys here understand my question.

Thank you so much!!

  • 写回答

3条回答 默认 最新

  • dpi96151 2013-12-18 15:38
    关注

    Please see my example code below on how to accomplish this.

    <?php
    $xml = <<<XML
    <videos>
        <video>
            <id>1</id>
            <enSub>Hello Foo! Good morning!</enSub>
            <cnSub>你好 Foo! 早上好!</cnSub>
        </video>
        <video>
            <id>2</id>
            <enSub>Hello Bar! Good afternoon!</enSub>
            <cnSub>你好 Bar! 下午好!</cnSub>
        </video>
    </videos>
    XML;
    // Lowercase the XML so we can do a non-case-sensitive search.
    $xml = strtolower($xml);
    // Create a DOMDocument based on the xml.
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    // Create an xpath based on the dom document so we can search it.
    $xpath = new DOMXpath($dom);
    // Search for any video tag that contains the text good morning.
    $nodes = $xpath->query('//video[contains(.,\'good morning\')]');
    // Iterate all nodes
    foreach($nodes as $node){
        // find the ID node and print its content.
        var_dump($xpath->query('id',$node)->item(0)->textContent);
    }
    

    -- Edit

    I reread your post and it looks like you're using keywords and not strings. If that's the case, then try this snippet on for size:

    <?php
    $xml = <<<XML
    <videos>
        <video>
            <id>1</id>
            <enSub>Hello Foo! Good morning!</enSub>
            <cnSub>你好 Foo! 早上好!</cnSub>
        </video>
        <video>
            <id>2</id>
            <enSub>Hello Bar! Good afternoon!</enSub>
            <cnSub>你好 Bar! 下午好!</cnSub>
        </video>
    </videos>
    XML;
    // Lowercase the XML so we can do a non-case-sensitive search.
    $xml = strtolower($xml);
    // Create an DOMDocument based on the xml.
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    // Create an xpath based on the dom document so we can search it.
    $xpath = new DOMXpath($dom);
    // Define the search keywords
    $searchKeywords = array('good','hello');
    // Iterate all of them to make them into valid xpath
    $searchKeywords = array_map(
        function($keyword){
            // Replace any single quotes with an escaped single quote.
            $keyword = str_replace('\'','\\\'',$keyword);
            return 'contains(.,\''.$keyword.'\')';
        },
        $searchKeywords
    );
    // Implode all the keywords using and, you could change this to be
    // an"or" condition if you so desire.
    $searchKeywords = implode(' and ',$searchKeywords);
    // The search keywords now look like contains(.,'good') and contains(.,'hello')
    // Search for any video tag that contains the text good morning.
    $nodes = $xpath->query('//video['.$searchKeywords.']');
    // Iterate all nodes
    foreach($nodes as $node){
        // find the ID node and print its content.
        var_dump($xpath->query('id',$node)->item(0)->textContent);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!