2010-11-09 46 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

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