2017-01-18 16 views
0
<a href="link">[BIOL 107 Section 1] Concepts in Biology</a></td> 
<a href="link">[CENG 230 Section 7] Introduction to C Programming</a> 
<a href="link">[CENG 230 All Sections] Introduction to C Programming</a></td> 

上記のサンプルは私のコードです。特定の単語preg_matchとregexを含む行を削除する

私は、含まれていないanchorsを取得しようとしていますALL。私はほとんどすべてを試して、正規表現の文書を見たが、私は何かを考え出すことができなかった。

+0

ここで質問は何ですか? _where_から行を削除しますか?これがPHPの場合、これを行うコードはどこにありますか? HTMLのいくつかの行は実際にここで続行するには十分ではありません。 – jdv

+0

あなたが試したことをお見せしてください。しかし、それは良い練習ではありません([またはいくつかの悪い](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454))))にHTMLを解析するにはregexpを使用します。 –

答えて

0

DOMDocumentを使用し、HTMLを解析するために正規表現を使用しないでください。

$html = ' 
<a href="link1">[BIOL 107 Section 1] Concepts in Biology</a> 
<a href="link2">[CENG 230 Section 7] Introduction to C Programming</a> 
<a href="link3">[CENG 230 All Sections] Introduction to C Programming</a> 
'; 
$dom = new DOMDocument; 
$dom->loadHTML($html); 
$tags = $dom->getElementsByTagName('a'); 
$links = array(); 
$value = array(); 
foreach($tags as $a){ 
    if (preg_match('/\ball\b/i', $a->nodeValue)) continue; 
    $links[] = $a->getAttribute('href'); 
    $value[] = $a->nodeValue; 
} 
print_r($links); 
print_r($value); 

出力:

Array 
(
    [0] => link1 
    [1] => link2 
) 
Array 
(
    [0] => [BIOL 107 Section 1] Concepts in Biology 
    [1] => [CENG 230 Section 7] Introduction to C Programming 
) 
+0

私はこれを答えとしてマークしました。あなたはどうすれば知ることができますか?{ –

+0

} {0} $ {nodeValue、$ matches}){ \t $ value [preg_match( '/ \ [[^ \]] * \]/] = $は[0]にマッチします; } 'これを加えて解決! –

0

この正規表現を試してみてください:

<a\s*[^>]*?>(?![^<]*?All).*?<\/a> 

Demo

+0

https://regex101.com/r/U27um9/3それは2つの一致を得ます:/ –

+0

私は私の答えを更新しました。 –

+0

[]とhrefの間のテキストのみを別々に取得する方法は分かりますか? –

関連する問題