Who can help write this regular expression? for example:
I want to match the class or id of an HTML tag .. (If there is an ID, take the ID. if there is no ID, take the CLASS)
It is better to match whether it is p, span, or other.
Reply to discussion (solution)
Provides a jQuery solution
Script
Function getTagInfo (e ){
Var str = '';
If (jQuery (e). attr ('id ')){
Str + = 'Id: '+ jQuery (e). attr ('id ');
} Else if (jQuery (e). attr ('class ')){
Str + = 'class: '+ jQuery (e). attr ('class ');
} Else {
Str + = 'neither id nor class attribute ';
}
Str + = '\ ntagName:' + jQuery (e). get (0). tagName;
Alert (str );
}
Script
Click here to try
Click here to try
I did not pay attention to writing it into the source code, which made the format messy. See the following:
Function getTagInfo (e) {var str = ''; if (jQuery (e ). attr ('id') {str + = 'Id: '+ jQuery (e ). attr ('id');} else if (jQuery (e ). attr ('class') {str + = 'class: '+ jQuery (e ). attr ('class');} else {str + = 'neither id nor class attribute set';} str + = '\ ntagName:' + jQuery (e ). get (0 ). tagName; alert (str );}
Click me
Click me
$ Html =''; $ Dom = new DOMDocument (); @ $ dom-> loadHTML ($ html); $ x = new DOMXPath ($ dom ); foreach ($ x-> query ("// p") as $ node) // If you want to span, // replace p with // span {echo $ node-> getAttribute ("id"); echo'
'; Echo $ node-> getAttribute ("class ");}
I didn't see it clearly. in the past, span or p was not necessarily the same, so I changed // * and then nodeName to get the HTML tag name.
';$dom = new DOMDocument();@$dom->loadHTML($html);$x = new DOMXPath($dom); foreach($x->query("//*") as $node) { echo $node->getAttribute("id"); //myidecho '
';echo $node->getAttribute("class"); //myclassecho '
';echo $node->nodeName; // p} ?>
[/Code]
Can PHPQUERY be used?
[/Code]
Can PHPQUERY be used?
Sorry! I have never touched PHPQUERY