WEB Anyone good with regex in php?

Discussion in 'OT Technology' started by o2, Jun 16, 2009.

  1. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    I need to extract # of hours, and minutes into 2 respective variables ($hours, $minutes) from the following examples

    Im not good with regex, can anyone help?
     
  2. projectalpha

    projectalpha 二號 Stunna

    Joined:
    Aug 4, 2001
    Messages:
    17,671
    Likes Received:
    0
    Location:
    Bay Area, California
    have you tried using strtotime()?
     
  3. crazybenf

    crazybenf Active Member

    Joined:
    Nov 14, 2001
    Messages:
    15,575
    Likes Received:
    2
    convert to time, use date functions.
     
  4. fishbulb

    fishbulb Active Member

    Joined:
    Oct 29, 2001
    Messages:
    6,848
    Likes Received:
    4
    Location:
    md
    Here's a hack-ish way to do it assuming the format doesn't deviate from what you posted:
    Works for lines like 5h also
    Code:
    <?php
    
    $lines = file('http://something.com/something.txt');
    
    
    $hours = 0;
    
    foreach ($lines as $line_num => $line) {
       
      if(strpos($line,'h')!== FALSE){
      
        $tempArray = explode('h',trim($line));
        
        if(count($tempArray) > 1){
        
          $hours+=$tempArray[0];
                
          $tempArray2 = explode(' ',trim($tempArray[1]));
          
          $hours+=($tempArray2[0] / 60);
          
        }else{
        
          $hours+=$temp[0];    
              
        } 
      
      }else{
      
          $tempArray3 = explode(' ',trim($line));    
          $hours+=($tempArray3[0] / 60);      
      
      }
    
    }
    
    echo $hours;
    
    ?>
    
     
  5. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
  6. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,650
    Likes Received:
    15
    Location:
    Atlanta
    something like that - :dunno:
     
  7. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Uh.... no.... I have html code, like this

    Code:
    <table width="930" border="0">
        <tr>
          <td style="font-size: 20px;">
    
    <strong>another russian teen named natasha</strong><strong> - </strong> 1h 18 min - <a href="http://upload.xvideos.com/?compte&favorite&add_favorite=13434"><u>Add this video to your XV favorites</u></a></td>
        </tr>
      </table>
      <table width="930" border="0">
        <tr>
          <td><em>Tagged</em> : <a href="/tags/anal">anal</a>, <a href="/tags/cumshot">cumshot</a>, <a href="/tags/facial">facial</a>, <a href="/tags/teen">teen</a>, <a href="/tags/blowjob">blowjob</a>, <a href="/tags/young">young</a>, <a href="/tags/threesome">threesome</a>, <a href="/tags/doublepenetration">doublepenetration</a>, <a href="/tags/pussyfucking">pussyfucking</a>, <a href="/tags/assfucking">assfucking</a>, <a href="/tags/russian">russian</a>,  more <a href="http://www.xvideos.com/tags/" style="text-decoration: underline;"><strong>tags</strong></a>. <span style="cursor: pointer; text-decoration:underline;" onClick="commentsDisplay()"><b>Show and post comment (37)</b></span></td>
        </tr>
    <tr>
    
    Need to extract "1h 18 min" and put it into 2 variables.
     
  8. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    nvm I got it

    Code:
    $needle = "#<strong> - </strong>(.h)? ?([0-9]?[0-9] min) - <a#";
    preg_match($needle,$haystack,$results);
    $hours = intval(preg_replace('/[^0-9]/i', '', $results[1])); 
    $minutes = intval(preg_replace('/[^0-9]/i', '', $results[2])); 
    
     

Share This Page