PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Is it possible to write a pattern to match a html tag with indeterminate number of attributes and return these attributes as array with attribute name as key and value as value???
I have tried the code above, but it's not working as intended.
If it'is impossible to achieve above array using only a single pattern, the array below also is accepted, thus I can use it to build an array as intended.
i built a funtion for something rather similar using the preg_split() function.
if you already have an attempt to do what you want with that, post it and i'll give you hints till you've succeded so you'll learn it. otherwise i suggest reading the link, then asking any questions necessary for clarification, because you can build a function to do it using preg_split.
if you're unsure of what that does, first check the functions on php.net, then ask here. everyone that knows regexp will probalby help. instead of giving you the thing i had varied to suit your needs i modified what you did since that's obvuiosly going to be something you'll pick up faster
Array
(
їname] => Ben
їemail] => ben@ben
їanother] => test of ' in string
їtest] => different quote
їanother_test] =>
)
Now the problem are tags that haven't quotes enclosing attribute's value, the value is missing... see another_test tag as example. Any ideas to solve???
I'm thinking about format the tag properly before use it... In this case I'll need a pattern to do it. e.g.: <user_data test="first_test" another_test=last_test/> after format: <user_data test="first_test" another_test="last_test"/> ... it seems impossible to do it using only a pattern, no?!??!..
it will be a little hard to get another_test=no quotes <-- part in bold, because it has no aparent ending, it has a start ("), but no ending... leaving the attribute open to any other attribute, thus eating any other strings..., but this can be achieved if the value after the = is a single word, no spaces
m3rajk wrote:did you try the variationi gave you yet?
Yes, I did... but it didn't return intended array.
m3rajk wrote:do you mind if it returns a multi-dimensional array?
Yes, but not in the intendend format.
does it get it to what you want? i didn't really think completely through on purpose...aand i think i know the issue... so if you take my idea and then find the forrect regexps to make it work ....
like i said, you're here to learn, so i don't reallllllly wanna give the exact csolution even if i know it. i think i know some ones that will help you, so i'll give them to you and let you decide which to use and how to use them
'/<([^>])>/' <-- gets everythin in a html tag
'/(\w+=".+")/' <--- gets each thing (however blah = "blah blah" wont work. for that you need a slight alteration: '/(\w+\s+=\s+".+")/' yet that requires " still. to not care about the " : '/(\w+\s+=.+^(\w+\s+=))/'
if you play with () you can get the initial one to find what's inthe <> to give you an array that's everything you want....