dse3168 2012-02-24 13:17
浏览 85
已采纳

PHP从列表中获取元素

I need to get elements from inside a list, i catch up an update from a single wordpress page in my post.php file trough $post->post_content

The output from it is:

Sportpaleis Antwerpen 25/02/2012 Sioen De Zwerver leffinge 24/03/2012 Nathan The zydeco Cha-Chas n9 Eeklo 24/03/2012 Kraantje Pappie Nijdrop Opwijk 24/03/2012 Eigen Makelij Hof Ter Loo Antwerpen 26/03/2012 test testPura vida Sportpaleis Antwerpen 25/02/2012 Sioen De Zwerver leffinge 24/03/2012 Nathan The zydeco Cha-Chas n9 Eeklo 24/03/2012 Kraantje Pappie Nijdrop Opwijk 24/03/2012 Eigen Makelij Hof Ter Loo Antwerpen 26/03/2012

I need to find a way to get every piece of text seperatly in this list. Can i do this by putting them between div ids? and how would i adress these divs? The point is that i need to export this content to an xml. I can put contents in an xml but the problem is getting this html text into seperate variables so i can put em between those tags example: HTML

<p id="artist">Sioen</p>
<p id="location">De Zwerver leffinge</p>
<p id="date>24/03/2012</p>

Should become: (xml)

<concerts>
<concert>
<artist>Sioen</artist>
<location>De zwerver leffinge</location>
<date>24/03/2012</date>
</concert>
.....
</concerts>

Since the comments suggest that i should put some kind of structure in i used a var_dump on the values with the <p id=""> </p> and the output is

Sioen

De Zwerver leffinge

24/03/2012
...
  • 写回答

2条回答 默认 最新

  • duanbi2003 2012-02-24 13:27
    关注

    The output from it is:

    If the output is literally what you said, it's not possible. I mean, I know that "Kraantje Pappie Nijdrop Opwijk" means that the artist is "Kraantje Pappie" and the location is "Nijdrop Opwijk", but that's because I'm Dutch and I happen to like "Kraantje Pappie". Most other people wouldn't even know where to split the string, so how do you want to tell your computer how to it?

    If the result is coming from the database, perhaps you can request it differently?

    UPDATE:

    Since the comments suggest that i should put some kind of structure in i used a var_dump on the values with the <p id=""> </p> and the output is

    Those linebreaks change everything. You can now operate under the assumption that there's a pattern in the string: artist, break, location, break, date, break. If there's a pattern, you can use that. Assuming that's a format you're always getting, you can do this quite simply:

    <?php
    $output = 'Sioen 
    
    De Zwerver leffinge
    
    24/03/2012 
    
    Nathan
    
    The zydeco Cha-Chas n9 Eeklo 
    
    24/03/2012 
    
    Kraantje Pappie 
    
    Nijdrop Opwijk 
    
    24/03/2012';
    
    /**
     * Remove empty lines.
     */
    $lines = array_map( 'trim', array_filter( explode( "
    ", $output ) ) );
    
    /**
     * This is for saving the values temporarily.
     */
    $i = 0;
    $entries = array( );
    $current = array( );
    
    /**
     * Loop through all the entries.
     */
    foreach( $lines as $line ) {
        /**
         * Add the current line to the current entry.
         */
        $current[] = $line;
    
        /**
         * If $i == 2, that was the date. Add $current to the stack, and reset it's contents.
         */
        if( $i ++ === 2 ) {
            $entries[] = $current;
            $current = array( );
            $i = 0;
        }
    }
    
    /**
     * Build XML root.
     */
    $concerts = new SimpleXmlElement( '<concerts></concerts>' );
    
    /**
     * Append each entry as a concert.
     */
    foreach( $entries as $entry ) {
        $concert = $concerts->addChild( 'concert' );
    
        $concert->addChild( 'artist', array_shift( $entry ) );  
        $concert->addChild( 'location', array_shift( $entry ) );    
        $concert->addChild( 'date', array_shift( $entry ) );    
    }
    
    /**
     * Output, finally.
     */
    var_dump( $concerts->asXml( ) );
    

    That results in the following output:

    <?xml version="1.0"?>
    <concerts>
        <concert>
            <artist>Sioen</artist>
            <location>De Zwerver leffinge</location>
            <date>24/03/2012</date>
        </concert>
        <concert>
            <artist>Nathan</artist>
            <location>The zydeco Cha-Chas n9 Eeklo</location>
            <date>24/03/2012</date>
        </concert>
        <concert>
            <artist>Kraantje Pappie</artist>
            <location>Nijdrop Opwijk</location>
            <date>24/03/2012</date>
        </concert>
    </concerts>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?