PHP 大写字母会爆炸?

发布于 2024-10-01 15:03:40 字数 2376 浏览 6 评论 0原文

我遇到这个问题...我想知道你们中的任何人是否有任何想法如何解决它?

我必须将课程名称、教师姓名和教室分开。

雅内克·曼德程序 405 Arv.võr.Tom KülaotsÕ 205

Progr.al。是课程名称,Janek Mander 是教师姓名,Õ 405 是教室。 Arv.võr。是课程名称,Tom KÜlaots 是教师姓名,Õ 205 是教室。

我必须将它们分开,以便我可以识别它们...可能进入数组

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

现在我有这个想法...如果我可以检测到大写字母并将该字符串替换为 #{uppercaseletter} 那么我可以爆炸它...Õ 405我可以用 Õ 来爆炸,因为每个教室前面都有一个 Õ。

好吧,Progrl.al.Janek ManderÕ 405...只有三个大写字母...而老师的名字总是第二个大写字母...有什么方法可以利用它来发挥我的优势还是我必须重写dom 脚本?


到目前为止整个代码...

<!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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-10-08 15:03:40

很棘手,但我会这样做:(在 for 循环内)

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;
    }
}

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;
    }
}
苦行僧 2024-10-08 15:03:40

我认为,如果您对输入数据有一些清晰的模式,您可以使用正则表达式找到更好的解决方案。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文