删除带有特定单词 preg_match 和正则表达式的行
Removing lines with specific word preg_match and regex
<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>
上面的示例是我的代码。
我正在尝试获取不包含 ALL 的 anchors
。我几乎尝试了所有方法,查看了正则表达式文档,但我无法想出一些可行的方法。
试试这个正则表达式:
<a\s*[^>]*?>(?![^<]*?All).*?<\/a>
不要使用正则表达式解析 HTML,请使用 DOMDocument:
$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
)
<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>
上面的示例是我的代码。
我正在尝试获取不包含 ALL 的 anchors
。我几乎尝试了所有方法,查看了正则表达式文档,但我无法想出一些可行的方法。
试试这个正则表达式:
<a\s*[^>]*?>(?![^<]*?All).*?<\/a>
不要使用正则表达式解析 HTML,请使用 DOMDocument:
$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
)