Page 1 of 1

Need a loop for this code

Posted: Tue Dec 15, 2009 10:27 am
by klevis miho
I have this code:

Code: Select all

        $findTd = '<td colspan="2">';
        $start_td_pos = strpos($extract, $findTd);  //73
    
        $find_end_td = '</td></tr>';
        $end_td_pos = strpos($extract, $find_end_td); //105
        
        $length_grab1 = $end_td_pos - $start_td_pos; 
        
        $extract_hour_channel = substr($extract, $start_td_pos, $length_grab1);
        
        echo $extract_hour_channel.'<br />';  //hour and name of the channel
But I want a loop to display all the strings that are inside $findTd and $find_end_td.

Any help would be appreciated

Re: Need a loop for this code

Posted: Tue Dec 15, 2009 1:11 pm
by Christopher
What do the strings that are inside $extract, $findTd and $find_end_td look like? Examples please.

Re: Need a loop for this code

Posted: Tue Dec 15, 2009 1:32 pm
by klevis miho
$extract holds a large html file.
But I solved the thing, here is my code:

Code: Select all

    while($i==0) {
        
        $findTd = '<td colspan="2">';
        $start_td_pos = strpos($extract, $findTd);  //73
    
        $find_end_td = '</td></tr>';
        $end_td_pos = strpos($extract, $find_end_td); //105
        
        $length_grab1 = $end_td_pos - $start_td_pos; 
        
        $value = substr($extract, $start_td_pos, $length_grab1);
        
        echo $value.'<br />';  //hour and name of the channel
        
        $start_new_extract = $end_td_pos + strlen($find_end_td);
        
        $extract = substr($extract, $start_new_extract);
        
        if(strstr($extract,$findTd)==FALSE) {
        $i=1;
        }
    }
This finds all strings that are inside <td colspan="2"> </td>

Re: Need a loop for this code

Posted: Tue Dec 15, 2009 1:56 pm
by AbraCadaver
Not an answer to your original question, but...

Code: Select all

preg_match_all('#<td colspan="2">(.+?)</td>#s', $extract, $matches);
print_r($matches[1]);

Re: Need a loop for this code

Posted: Wed Dec 16, 2009 12:00 am
by montyhar2
I will be try my beat into this code.I have need inside $findTd and $find_end_td string function.My code is here.

preg_match_all('#<td colspan="2">(.+?)</td>#s', $extract, $matches); And print_r($matches[1]);

I will also try this code.This is best solution.Thanks.

Re: Need a loop for this code

Posted: Wed Dec 16, 2009 1:53 am
by klevis miho
Thank you for your code AbraCadaver, will try it and give you my feedback

Re: Need a loop for this code

Posted: Wed Dec 16, 2009 3:49 am
by klevis miho
How can I print just the values?

Re: Need a loop for this code

Posted: Wed Dec 16, 2009 7:41 am
by klevis miho
With this code:

1. preg_match_all('#<td colspan="2">(.+?)</td>#s', $extract, $matches);
2. print_r($matches[1]);

you will get not just the things inside the td's but also the td's.
How can I just get the strings inside the td's?

Re: Need a loop for this code

Posted: Wed Dec 16, 2009 8:56 am
by AbraCadaver
$matches[0] contains the tds with values, $matches[1] contains just the values inside the tds.

Re: Need a loop for this code

Posted: Wed Dec 16, 2009 9:16 am
by klevis miho
Thnx AbraCadaver it worked :D