Home > Backend Development > PHP Tutorial > 哪位高手帮忙写一下这个正则

哪位高手帮忙写一下这个正则

WBOY
Release: 2016-06-13 12:46:46
Original
982 people have browsed it

谁帮忙写一下这个正则

例如

 

我要匹配出来一个HTML标签的class 或者 id 是什么。。(有ID拿ID,没有就拿CLASS)
最好也能匹配出来到底是div还是span还是其它
我要匹配出来一个HTML标签的class?或者?id?是什么。。(有ID拿ID,没有就拿CLA..." data-pics="">
------解决方案--------------------
刚刚没注意将其写入到源代码中,使得格式错乱,看如下:
<br />
function getTagInfo(e){<br />
	var str='';<br />
	if(jQuery(e).attr('id')){<br />
		str += 'id:'+jQuery(e).attr('id');<br />
	}else if(jQuery(e).attr('class')){<br />
		str += 'class:'+jQuery(e).attr('class');<br />
		<br />
	}else{<br />
		str += '既没设置id也没设置class属性';<br />
	}<br />
	str += '\ntagName:'+jQuery(e).get(0).tagName;<br />
	alert(str);<br />
}<br />
Copy after login

<br />
<div id='myDivId' class="myDivClass" onclick="getTagInfo(this)">点我</div><br />
<span id='mySpanId' class="mySpanClass" onclick="getTagInfo(this)">点我</span><br />
Copy after login

------解决方案--------------------
我也没看清楚,原来span或者div是不一定的,那就换 //* 然后 nodeName 获取HTML标签名。

<br />
<?php<br />
$html = '<div id="myid" class="myclass"></div>';<br />
$dom = new DOMDocument();<br />
@$dom->loadHTML($html);<br />
$x = new DOMXPath($dom); <br />
<br />
foreach($x->query("//*") as $node) <br />
{<br />
    echo $node->getAttribute("id"); //myid<br />
	echo '<br />';<br />
	echo $node->getAttribute("class"); //myclass<br />
	echo '<br />';<br />
	echo $node->nodeName; // div<br />
} <br />
?><br />
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template