Jump to content

Help with regular expression


ngreenwood6

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/209938-help-with-regular-expression/
Share on other sites

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

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.

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.