ngreenwood6 Posted August 5, 2010 Share Posted August 5, 2010 Ok so here is the issue. I am trying to parse something out of a string that I provide. Basically the strings will look like this: {display="something"} {display="something_else"} {display=""} What I would like to do is be able to match against the display. Then if that exists to then find what the data in between the quotes is and set it into a variable. I was just doing a simple str_replace but I would like to make it dynamic so that anything can be entered there and it will work. I was hoping there is a method without using a regular expression (dont know them very well) but if not that is fine. Any help is appreciated. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 6, 2010 Author Share Posted August 6, 2010 Anyone? Quote Link to comment Share on other sites More sharing options...
awjudd Posted August 6, 2010 Share Posted August 6, 2010 Firstly ... you gotta give it more than 50 minutes before bumping ... that is just rude. What are you expecting within the quotes? Letters? Numbers? etc ... /\{display="([A-Z_]*)"\}/i Should give you what you want based on the sample. http://rubular.com/r/rk7rKB7jRo ~juddster Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 6, 2010 Share Posted August 6, 2010 Another thing, is display the name of the variable, or will that be static throughout? The way I'd go about it, if display is going to be the variable name. $string = 'display="hello_world"'; if(preg_match('#\{(\w+)="(\w+)"\}#', $string, $match)){ list($var, $val) = $match; $$var = $val; echo $display; // echos hello_world } Not tested, and it's also not as good as you could make it. See the variable name is matching alphanumeric characters, so you could enter 000="hello_world" and it would match. Probably better off making that into a character class. Depends how you're doing it. Quote Link to comment 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.