Jump to content

[SOLVED] Get tag attribute values


The Little Guy

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/159930-solved-get-tag-attribute-values/
Share on other sites

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?

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);

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.