ngreenwood6 Posted March 28, 2011 Share Posted March 28, 2011 I have some code that I would like to place in a file like so : {block type="left_side" name="Left Side"} What I want to be able to do is parse out each of the attributes for this. So I want to know what the type is and what the name is. I want it to be expandable though so that I can add on to it later on if I decide to add another attribute. Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/ Share on other sites More sharing options...
ManiacDan Posted March 28, 2011 Share Posted March 28, 2011 This is usually done with regular expressions, I imagine. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193270 Share on other sites More sharing options...
Maq Posted March 28, 2011 Share Posted March 28, 2011 Is it possible to add the code in as XML. That way, you could use XSLT to easily extract/add information. Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193273 Share on other sites More sharing options...
ngreenwood6 Posted March 28, 2011 Author Share Posted March 28, 2011 @ManiacDan - I know this is done with regular expressions which is why I posted in the regex forum lol. @Maq - I am not sure what you mean can I add the code as xml. Do you mean do the block like <block type="left_side" name="Left Side">? or do you mean actually create a separate xml file for this? Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193279 Share on other sites More sharing options...
ManiacDan Posted March 28, 2011 Share Posted March 28, 2011 @ManiacDan - I know this is done with regular expressions which is why I posted in the regex forum lol. What have you tried so far? This is relatively simple to parse just this tag. Parsing multiple tags in the same document which may or may not be nested or malformed is significantly harder, to the point where regex might not be able to do it. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193282 Share on other sites More sharing options...
ngreenwood6 Posted March 28, 2011 Author Share Posted March 28, 2011 The regex that I used to pull in the tags was "@{block(.*)}@" which works but doesnt pull in all of the attributes individually. Now I know how to program pretty much anything and am really well versed in php but I am not good at regular expressions at all. Which I will be working on for the future. Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193285 Share on other sites More sharing options...
Maq Posted March 28, 2011 Share Posted March 28, 2011 I think creating an XML file for this and using XSL would be much more maintainable and efficient. As long as the file is valid XML then you can parse it with something like SimpleXML in PHP or use an XSL sheet. Here are a few helpful links: http://cowburn.info/2009/10/23/php-funcs-xpath/ http://us.php.net/simplexml Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193291 Share on other sites More sharing options...
ngreenwood6 Posted March 28, 2011 Author Share Posted March 28, 2011 Sorry Maq but that solution will not work for what I am trying to accomplish. I am thinking that I may just store an array of parsable attributes and then loop through them and get there values. This way later on I can just add the value to the array and it will pull the attribute that I need it to anyways. Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193294 Share on other sites More sharing options...
ManiacDan Posted March 28, 2011 Share Posted March 28, 2011 It's sloppy to use two, but I'm going to lunch and can't be bothered. I'm pinging requinix though, it's like the bat signal, only for regex: $string = '{block type="left_side" name="Left Side"}'; preg_match('/\{block ([^\}]+)\}/', $string, $foo); preg_match_all('/([^=]+)="([^"]+)"\s*/', $foo[1], $bar); foreach ( $bar[1] as $k => $v ) { echo "{$v} = {$bar[2][$k]} <br />\n"; } -Dan Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193311 Share on other sites More sharing options...
.josh Posted March 28, 2011 Share Posted March 28, 2011 I do not recommend using regex for this. As Maq suggested, use xml. If that is something you can't do, then can you explain what you are ultimately doing with these values, how you are parsing and using them later? Another option is to store as serialized array or a JSON object string. But again, it depends on what you are doing with it later on. But if you are able to parse with regex later on, I really don't see why you can't do one of these other solutions. Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193317 Share on other sites More sharing options...
requinix Posted March 28, 2011 Share Posted March 28, 2011 If you know the attributes ahead of time then there's a very simple solution: $string = 'foo {block attr1="value 1 with a {" attr2="value 2 with a }"} bar'; preg_match_all('/ \{block\s+( attr1="(?P[^"]*)"| # attribute attr2="(?P[^"]*)"| # attribute \w+="[^"]*"| # catch-all for other attributes \s # spaces between attributes )+\}/ix', $string, $matches, PREG_SET_ORDER); var_dump($matches); foreach ($matches as $i => $block) { echo "block ", $i + 1, ": attr1 = '", $block["attr1"], "', attr2 = '", $block["attr2"], "' \n"; } I'm not sure if I would suggest XML. It depends on where you're putting these tags. In HTML? Then yes, XML is a great option. But you can't really stick XML willy-nilly in, say, plaintext files... Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193320 Share on other sites More sharing options...
.josh Posted March 28, 2011 Share Posted March 28, 2011 so what happens if the value has a " in it? Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193329 Share on other sites More sharing options...
ngreenwood6 Posted March 28, 2011 Author Share Posted March 28, 2011 thanks for the help guys i appreciate it. maybe if i help better explain what I am trying to do we can come up with a better solution. basically what I am trying to do is create a template system for the pages. I want to be able to create "blocks" that content can be laid into. However, I will need to know what these blocks are by there type but wanted to give them a friendly name to view them by. So an example would be something like this: <html> <head> </head> <body> <div id="wrapper"> {block type="left_side" name="Left Side"} {block type="content" name="Content"} {block type="right_side" name="Right Side"} </div> </body> </html> So basically then I will be able to assign things to the left side, content, and right side and be able to display them in place of the blocks. I want to keep it simple to use but at the same time I want to be able to accomplish what is needed. If this can be done similiar to this but easier to manage then I am open to suggestions. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193333 Share on other sites More sharing options...
ManiacDan Posted March 28, 2011 Share Posted March 28, 2011 Why do this at all? Why not simply use PHP variable names? <div id="wrapper"> {$leftSideBlock} {$contentBlock} {$rightSideBlock} </div> You will have a fixed set of content blocks. str_replace will do this for you easily. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193429 Share on other sites More sharing options...
ngreenwood6 Posted March 28, 2011 Author Share Posted March 28, 2011 What are you talking about? Those blocks are going to be based on whatever I need for the template so its not always going to be those. I am not simply using variable names because like I said I wish to have attributes that can be assigned to the elements. Your code doesnt make any sense anyways cause I wouldnt need the $ in the name so it could just be (leftSideBlock vs $leftSidBlock) considering the fact that I am not in php tags. And like I said I am not a noob php developer so I know that str_replace would work for your example but I am looking for something more complex which is why i posted in the regex forum. Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193451 Share on other sites More sharing options...
ManiacDan Posted March 29, 2011 Share Posted March 29, 2011 Ok, a number of things: 1) You could very well use the PHP variable syntax and run it back through the PHP parser to let PHP handle your interpolation, but that's dangerous and unnecessary for such a simplistic task. 2) You're honestly saying that you will not know ahead of time what these content blocks will be named? They could be anything? How are you planning on generating code if someone drops in a content block like {block type="my_new_block" name="The Page"} ? What will your code do? Maybe that's the question we should have asked from the beginning. How will you be filling in custom block names? If you have a database table full of content block names...we're back to my str_replace suggestion. 3) Just because it's regex doesn't mean it's more complicated or more correct. Regex is a last resort. It's slow, cumbersome, confusing, and prone to errors. XML parsing would work better if this is pure namespaced HTML (which it appears to be) and str_replace will be the absolute fastest route. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/231956-help-with-getting-attributes/#findComment-1193680 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.