need help with Explode function

Discussion in 'PHP' started by coladeu, May 6, 2012.

  1. #1
    I need a little help with this.. i want to extract the playtime between span but i have a problem with title="PT because is changed on every span and i don't know how to replace that number to be able to extract all.
    This is the html code.
    Here is the code for extraction
    $playtime = explode("<span class=\"duration\" [B]title=\"PT"[/B], $track);
    $playtime = explode("</span>", $playtime[1]);
    $playtime = $playtime[0];
    
    Code (markup):
    and this are the results
    I need to get rid of the 301"> parts.
     
    Last edited: May 6, 2012
    coladeu, May 6, 2012 IP
  2. infotripro

    infotripro Member

    Messages:
    26
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    43
    #2
    $mystring='<span class="duration" title="PT301">3.01</span>';
    $pos = strpos($mystring, ">");


    $strright=substr ($mystring , $pos+1, strlen($mystring)-$pos-1);


    $result=str_replace("</span>","",$strright);
     
    infotripro, May 6, 2012 IP
  3. coladeu

    coladeu Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks infotripro
     
    coladeu, May 6, 2012 IP
  4. goldenretriever

    goldenretriever Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    coladeu,

    Here is another option using preg_match_all (regular expressions):

    $string="<span class=\"duration\" title=\"PT301\">3.01</span>
    <span class=\"duration\" title=\"PT236\">2.36</span>
    <span class=\"duration\" title=\"PT433\">4.33</span>";

    preg_match_all("|<span class=\"[^\"]*\" title=\"PT[^\"]*\">([^<]*)</span>|is",$string,$c);
    print_r($c);
     
    Last edited: May 6, 2012
    goldenretriever, May 6, 2012 IP