Need a loop for this code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Need a loop for this code

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Need a loop for this code

Post by Christopher »

What do the strings that are inside $extract, $findTd and $find_end_td look like? Examples please.
(#10850)
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Need a loop for this code

Post 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>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Need a loop for this code

Post 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]);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
montyhar2
Forum Newbie
Posts: 3
Joined: Mon Dec 14, 2009 9:18 am

Re: Need a loop for this code

Post 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.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Need a loop for this code

Post by klevis miho »

Thank you for your code AbraCadaver, will try it and give you my feedback
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Need a loop for this code

Post by klevis miho »

How can I print just the values?
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Need a loop for this code

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Need a loop for this code

Post by AbraCadaver »

$matches[0] contains the tds with values, $matches[1] contains just the values inside the tds.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Need a loop for this code

Post by klevis miho »

Thnx AbraCadaver it worked :D
Post Reply