我需要匹配所有这些开始标记:
<p>
<a href="foo">
Run Code Online (Sandbox Code Playgroud)
但不是这些:
<br />
<hr class="foo" />
Run Code Online (Sandbox Code Playgroud)
我想出了这个,并希望确保我做对了.我只抓住了a-z.
<([a-z]+) *[^/]*?>
Run Code Online (Sandbox Code Playgroud)
我相信它说:
/,然后我有这个权利吗?更重要的是,你怎么看?
我需要从下面的html代码中获取h2和h3标签为PHP中的$ var:
<div class="main-info">
<img class="iphone-img" alt="" src="https://www.myweb.com/securedImage.jsp">
<div class="sub-info">
<h2 class="model">iPhone 4S</h2>
<h3 class="capacity color">16GB Black</h3>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我想要这个结果:
echo $model; // Should echo: 'iPhone 4S'
echo $capacitycolour; // Should echo: '16GB Black'
Run Code Online (Sandbox Code Playgroud)
我试图用preg_match,preg_match_all和getElementsByTagName但至今没有运气.
这是我试过的代码:
$pattern = '/[^\n]h2*[^\n]*/';
preg_match_all($pattern,$data, $matches, PREG_OFFSET_CAPTURE);
var_dump($matches);
Run Code Online (Sandbox Code Playgroud)
和:
$doc = new DOMDocument();
$doc->loadHTML($data);
$tags = $doc->getElementsByTagName('sub-info');
$root = $doc->documentElement;
foreach($root->childNodes as $node){
$attributes[$node->nodeName] = $node->nodeValue;
}
var_dump($attributes);
Run Code Online (Sandbox Code Playgroud)