2010-11-09 47 views
1

即時通訊存在這個問題...我想知道你有沒有任何想法如何解決它?PHP由大寫字母分解?

我必須分開課程名稱,教師姓名和課堂。

Progr.al.JanekManderÕ405 Arv.võr.TomKülaotsÕ205

Progr.al。是課程名稱,Janek Mander是教師姓名,並且405是課堂。 Arv.võr。是課程名稱,TomKÜlaots是教師姓名,Õ205是課堂。

我要它們分開這樣我就可以辨別出來......也許到數組

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

現在我有這個想法,如果我發現大寫字母與和#{uppercaseletter替換字符串}然後我可以爆炸它...... 405我可以通過Õ爆炸,因爲每個教室都有一個Õ在他們面前。

那麼Progrl.al.JanekManderÕ405 ......只有三個大寫字母......老師的名字總是第二個大寫字母......有沒有什麼辦法可以用我的advatage或做我必須重寫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> 
+0

個人而言,我會建議放在一起的數據的初審一個更有組織的形式,依靠正則表達式和(for環內) 'preg_split()'](http://www.php.net/manual/en/function.preg-split.php)(使用正則表達式的'explode()')很脆弱。 – 2010-11-09 23:15:00

+0

在dev-null-dweller給了我一個解決方案之前,我使用了這個: $ TestStr =「Tom Kulaots」; $ s = preg_replace('/([^ \ s])([A-Z])/','\ 1#\ 2',$ TestStr); $ info = explode(「#」,$ s); $ TestStr = $ info [1]; $ info = explode(「Õ」,$ TestStr); $ name = $ info [0]; – 2010-11-12 00:42:35

回答

1

棘手,但我會做這種方式:

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

工程就像一個魅力。謝謝。 – 2010-11-12 00:38:12

0

我認爲,如果你有你的輸入數據的一些清晰的模式,你可以接近使用正則表達式更好的解決方案。