Ardi Vaba
Ardi Vaba

Reputation: 25

PHP Explode by Uppercase letter?

im having this problem...i wonder if any of you have any ideas how to solve it?

I have to seperate Lesson Name, Teacher name and Classroom.

Progr.al.Janek ManderÕ 405 Arv.võr.Tom KülaotsÕ 205

Progr.al. is Lesson name, Janek Mander is Teacher name and Õ 405 is Classroom. Arv.võr. is Lesson name, Tom KÜlaots is Teacher name and Õ 205 is Classroom.

I have to seperate them so i can identify them...probably into array

info[0] = "Progr.al."
info[1] = "Janek Mander"
info[2] = "Õ 405"

Now i have this idea...if i can detect uppercase letter and replace that string with and #{uppercaseletter} then i could explode it...Õ 405 i can explode by Õ as an every classroom has an Õ before them.

Well Progrl.al.Janek ManderÕ 405...there are only three uppercase letters...and teachers first name has always the second uppercase letter...is there any way i could use that to my advatage or do i have to rewrite dom script?


Whole code so far...

<!doctype html>
<html>
<head>
    <title>Ilus tunniplaan</title>
    <style>
        .tund
        {
            width: 140px;
            width: 405px;
            border: 1px solid black;
        }
        .
    </style>
</head>
<body>
<?php
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'Off');
ini_set('log_errors', 'Off');

function grab_page( $site )
{
    $ch = curl_init( );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
    curl_setopt( $ch, CURLOPT_TIMEOUT, 40 );
    curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
    curl_setopt( $ch, CURLOPT_URL, $site );
    ob_start( );
    return curl_exec ( $ch );
    ob_end_clean( );
    curl_close ( $ch );
}

$html = grab_page( "http://web.ametikool.ee/tunniplaan/11.%20n%e4dal%2008.11%20-%2013.11/" );

 $dom = new domDocument; 
    /*** load the html into the object ***/ 
    $dom->loadHTML($html); 

    /*** the table by its tag name ***/ 
    $tables = $dom->getElementsByTagName('table'); 

    /*** get all rows from the table ***/ 
    $rows = $tables->item(0)->getElementsByTagName('tr'); 

    /*** loop over the table rows ***/ 
    foreach ($rows as $row) 
    {
        $id = $id + 1;
        if( $id > 16 )
        {
            /*** get each column by tag name ***/ 
            $cols = $row->getElementsByTagName('td'); 
            /*** echo the values ***/ 
            for ( $counter = 0; $counter <= 9; $counter += 1) 
            {
                $phrase  = $cols->item($counter)->nodeValue;
                echo $phrase . "<br/>\n";
            }
        }
    }
?>
</body>
</html>

Upvotes: 1

Views: 766

Answers (2)

dev-null-dweller
dev-null-dweller

Reputation: 29462

Tricky, but I would do it this way: (inside of for loop)

for ( $counter = 0; $counter <= 9; $counter += 1)
{
    $phrase  = $cols->item($counter);

    $breaklines = $phrase->getElementsByTagName('br');
    if($breaklines->length == 2)
    {
        $br = array();
        for($i=0;$i<2;$i++)
        {
            $br[$i] = $breaklines->item($i);
        }
        //Don't try to put this two for-loops into one.
        for($i=0;$i<2;$i++)
        {
            $phrase->replaceChild($dom->createTextNode('|'), $br[$i]);
        }

        print_r(explode('|',$phrase->nodeValue)) . PHP_EOL;
    }
}

Upvotes: 1

denica
denica

Reputation: 221

I think that if you have a some clear pattern of your input data you could approach a better solution using regular expressions.

Upvotes: 0

Related Questions