Jump to content

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.

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.