The Little Guy Posted May 27, 2009 Share Posted May 27, 2009 I have a file that has tags like this in it: <clp name="title" type="text"></clp> or <clp name="content" type="textarea"></clp> attributes in the tags can be reversed just like in HTML files, I may add more attributes, but for now this is all I have. I need to find all these attributes, and get their values in these tags, so How can I do it? Currently I have this to search: preg_match_all("~<clp (type|name)=\"(.+?)\"><\/clp>~",$contents, $matches); but... it doesn't work, it worked before I added "type" to the pattern though. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/159930-solved-get-tag-attribute-values/ Share on other sites More sharing options...
Axeia Posted May 27, 2009 Share Posted May 27, 2009 I don't see how adding type would kill it off if it worked with name before. The order of your backreferences would however changes as the round () brackets mean you're capturing whatever matches inside of it. (Which is prolly what you want if you want to be able to tell a name apart from a type) Isn't the script processing the output of this regex the one at fault? Did you do a print_r( $match ) to see if it's in the format you're expecting? Quote Link to comment https://forums.phpfreaks.com/topic/159930-solved-get-tag-attribute-values/#findComment-843583 Share on other sites More sharing options...
.josh Posted May 28, 2009 Share Posted May 28, 2009 IMO it would be "less complex" to break it down into several expressions. Example: $string = '<clp name="title" type="text"></clp>'; preg_match('~<clp ([^>]*)>~i',$string,$tags); preg_match_all('~([a-z]+)="([^"]*)"~i',$tags[1],$attribs); echo "<pre>"; print_r($attribs); Quote Link to comment https://forums.phpfreaks.com/topic/159930-solved-get-tag-attribute-values/#findComment-843711 Share on other sites More sharing options...
The Little Guy Posted May 28, 2009 Author Share Posted May 28, 2009 Thanks! that works Quote Link to comment https://forums.phpfreaks.com/topic/159930-solved-get-tag-attribute-values/#findComment-843785 Share on other sites More sharing options...
Daniel0 Posted May 28, 2009 Share Posted May 28, 2009 Why don't you just use an XML parser? Quote Link to comment https://forums.phpfreaks.com/topic/159930-solved-get-tag-attribute-values/#findComment-843941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.