2012-02-21 137 views
-1

我正在一個項目,我必須屏幕刮網站並獲得一個字符串。這是文本的一部分。正則表達式匹配模式之前得到字符串

A HREF = 「/儀表板/指數/ 2971」 標題= 「PROJECT1:PROJECT1」> PROJECT1

我需要得到 「/儀表板/指數/ 2971」 整體的一部分使用正則表達式的文本。目前我有這個:

while(true){ 
       if (buff.readLine()!=null){ 
        String wholeText = buff.readLine(); 
        System.out.println(wholeText.contains("title=Project1")); 
        htmlCode += buff.readLine() + "\n"; 
       }else{ 
        break; 
       } 

這只是標識「title = Project1」字符串。我需要獲取「/ dashboard/index/2971」部分並將其放入一個字符串中。

回答

0
<?php 
$str = 'a href = "/dashboard/index/2971" title="Project1:Project1">Projeca...'; 

preg_match_all('#href\s*=\s*"(.*?)"#', $str, $matches, PREG_SET_ORDER); 

$foundURLs = array(); 
foreach ($matches as $match) { 
    $foundURLs[] = $match[1]; 
} 

var_dump($foundURLs); 
相關問題