aximbigfan Posted August 27, 2008 Share Posted August 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121519-need-a-regexp-pattern/ Share on other sites More sharing options...
effigy Posted August 27, 2008 Share Posted August 27, 2008 Try /^[^=\s]+\s*=\s*.+$/m. Quote Link to comment https://forums.phpfreaks.com/topic/121519-need-a-regexp-pattern/#findComment-626855 Share on other sites More sharing options...
aximbigfan Posted August 27, 2008 Author Share Posted August 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121519-need-a-regexp-pattern/#findComment-626949 Share on other sites More sharing options...
effigy Posted August 27, 2008 Share Posted August 27, 2008 Will you be making many changes? parse_ini_file may be the better approach. Quote Link to comment https://forums.phpfreaks.com/topic/121519-need-a-regexp-pattern/#findComment-626955 Share on other sites More sharing options...
aximbigfan Posted August 27, 2008 Author Share Posted August 27, 2008 No, I need to write the values, not read them. Basically, the function is going into the file, finding the directive with the pattern, than replacing the directive's setting with a new one, by replacing the entire line. Thanks, Chris Quote Link to comment https://forums.phpfreaks.com/topic/121519-need-a-regexp-pattern/#findComment-627308 Share on other sites More sharing options...
nrg_alpha Posted August 27, 2008 Share Posted August 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121519-need-a-regexp-pattern/#findComment-627329 Share on other sites More sharing options...
aximbigfan Posted August 28, 2008 Author Share Posted August 28, 2008 Thanks! I see a way I may be able to do what I want with explode(). Chris Quote Link to comment https://forums.phpfreaks.com/topic/121519-need-a-regexp-pattern/#findComment-627422 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.