Jump to content

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

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.