doulupian8725 2014-05-28 11:21
浏览 62
已采纳

html刮和循环的字符串

The question is very simple, im doing some html scraping to get bus timelines using php. i have the following string:

Fermata 1179 - Linea 203 -> 13:12 Linea 254 -> 13:19 Linea 203 -> 13:20 Linea 1 -> 13:29

Fermata 1179 is the id of the stop, and its quite easy to get with substr()

what i cant seem to do is cycling through the Lines es:

Linea 203 -> 13:12

Linea 254 -> 13:19

Linea 203 -> 13:20

Linea 1 -> 13:29

i want to display as

Linea 203 at 13:12

Linea 254 at 13:19

Linea 203 at 13:20

Linea 1 at 13:29

what i've done so far ( i know its shit code but i have to start in some way :( )

<?php if ( $scraped_data != 'Previsioni in fermata non disponibili.'){

        // stringa: Fermata 1494 - Linea 202 -> 09:28
        echo 'dati:<br/>'.$scraped_data;    
        $x= 0;


        $idpalina =  substr($scraped_data,8,4); // ID PALINA
        echo '<br />id palina: '.$idpalina;
        echo '<br />posizione di " - ": '.strpos($scraped_data, ' - ');
        echo '<br />';

        // MI PRENDO IL NOME DELLA LINEA
        echo '<br />'.substr($scraped_data,strpos($scraped_data, ' -  ')+2,strpos($scraped_data, ' -> ')-(strpos($scraped_data, ' -  ')+2)); 
        //L ORA DELLA LINEA
        echo '<br />'.substr($scraped_data,strpos($scraped_data, ' -> ')+4,5); 

        // LA POSIZIONE DEL SEPARATORE PER L'ORA
        echo '<br />posizione di " ->": '.strpos($scraped_data, ' -> ');

        // CERCA LA PROSSIMA POSIZIONE DI LINE
        $x= strpos($scraped_data, '->') +9;
        echo  '<br />x= '.$x;
        echo  '<br />strlen= '.strlen($scraped_data);
        echo  '<br />i='.$i;
        // MI PRENDO IL NOME DELLA LINEA
        echo '<br />'.substr($scraped_data,strpos($scraped_data, ' -  ')+$x,strpos($scraped_data, ' -> ')-(strpos($scraped_data, ' -  ')+$x)); 
        echo '<br />'.
        }
        else
        echo 'Previsioni non disponibili';
        ?>

hope someone wil help me as i'm riving mad :(

  • 写回答

2条回答 默认 最新

  • doutang1873 2014-05-28 11:28
    关注

    You can use explode to break up the string as required:

    $string = 'Fermata 1179 - Linea 203 -> 13:12 Linea 254 -> 13:19 Linea 203 -> 13:20 Linea 1 -> 13:29';
    
    $parts = explode('Linea', $string);
    
    //start from 1, as the 1st item in array is 'Fermata 1179 - '
    for($i=1;$i<count($parts);$i++){
        echo '<p>Linea ' . str_replace('->', 'at', $parts[$i]) . '</p>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值