Jump to content

Need a regExp pattern


aximbigfan

Recommended Posts

Hi,

 

I am not very good at regExp. I need a pattern that will find a line like this

 

$somevar1 = "randomstring"

 

The first var name may have any chars in it (except '=' and ' ') then there will be some spaces, and '=' then a second string in between ""s .

 

This will be used in a function that finds a setting in an INI file, and replaces it with a new setting.

 

For example,

preg_replace("pattern with variable name in it", "the new string", "the string");

 

Thanks!

 

Chris

Link to comment
https://forums.phpfreaks.com/topic/121519-need-a-regexp-pattern/
Share on other sites

Thanks!

 

Is there a spot where I can put a varitable, and it will find that directive in the haystack?

 

Say the "input var" is "option_address". The haystack will look like this:

 

opt_port = "123"

opt_address = "MV2"

opt_auth = "test"

 

I'll use preg_replace to use that pattern and try to find "opt_address"'s directive, then remove that whole line, and put a new one in it's place, with a new setting.

 

I have everything else figured out, I just need a pattern I can plug a var into, then have it find the directive.

 

Thanks alot for your help!

 

Chris

Open the file with file() command (this opens and puts its contents into an array), which you can then use foreach and loop through each and using your rexeg pattern, make any changes per array keys, then implode the array and use file_put_contents to overwrite, save and close the file in one fell swoop. I think file() breaks file contents up via carriage return / newline if I'm not mistaken. So the array will probably depend on your file's structure.

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.