Jump to content

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.

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.