Who can help write this regular expression-php Tutorial?

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.