簡體   English   中英

php 從 HTML 中獲取所有樣式表標簽中的 href 值的正則表達式是什么?

[英]What is the regex for php to get the href value in all stylesheet tags from HTML?

使用 regex 和 preg_match_all 獲取所有 href 標簽以獲取給定標簽的 href 值的一般方法是什么並不總是按順序排列。

例子:

<link href="foo.css" rel="stylesheet" type="text/css"/>
<link type="text/css" href="bar.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="bar1.css"/>
<link type="text/css" href="bar2.css" rel="stylesheet"></link>
<link href="path/foo.css" rel="stylesheet" type="text/css"/>

應該導致:

Array(
'foo.css',
'bar.css',
'bar1.css',
'bar2.css',
'path/foo.css',
)

解析是要走的路:

$x = file_get_contents("foo.txt");
$xml = simplexml_load_string("<links>$x</links>");
$results = array();

foreach ($xml->link as $link)
    $results[] = (string)$link['href'];

看到它的工作: https : //eval.in/132898

您要查找的正則表達式類似於以下內容,但需要進一步細化:

<link\s+(?:[^>]*?\s+)?href="([^"]*)"

測試針對

<link href="foo.css" rel="stylesheet" type="text/css"/>

返回值為

<link href="foo.css"

這是測試表達式的好地方: http : //regexpal.com/

我會說:

preg_match_all('/href=\"([a-z1-9\/.]+)\"/img', $head, $matches)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM