loco41211 Posted April 29, 2010 Share Posted April 29, 2010 To simplify the scenario: I have a php script that opens given files and checks the value of a set of matching variables in that file. The line of code that extracts the variable is: $extr=substr($line,(strpos($line,'=')+3),(strrpos($line,'"')-(strpos($line,'=')+3))); The problem with this code is that it only works with code like this: $variable = "hello"; If there are more or less than 3 strpos, or whatever you want to call it, for example: $variable="hello"; or $variable = "hello"; then the script is useless and can't finish the job its meant to do. Could anybody suggest an improvement? or a way around this? ANY suggestions or ideas are welcome, post away! Addition: Someone else said regex it...also a better way is to just build in change checkers that check if lines were moved or if there aren't the proper names in place and make it so the rest of the code is reliant on these specific requirements and if a change is made then the script will break. would anybody here know what to make of this? I'm not good at all with regex... Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2010 Share Posted April 29, 2010 Assuming this is for a 'configuration file' exercise, if you just include() the file, the variables will be set with the current value. There is no need to actually parse through the file to find the variables and values. Using an array $settings['something1'] = 'value1'; $settings['something2'] = 'value2';will make the code simpler. Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/#findComment-1050618 Share on other sites More sharing options...
jdavidbakr Posted April 29, 2010 Share Posted April 29, 2010 If including the file isn't a viable option, you might try regular expressions. Check out preg_match or preg_match_all, one of those should give you the tools you need to parse that kind of data. Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/#findComment-1050620 Share on other sites More sharing options...
loco41211 Posted April 29, 2010 Author Share Posted April 29, 2010 If including the file isn't a viable option, you might try regular expressions. Check out preg_match or preg_match_all, one of those should give you the tools you need to parse that kind of data. It sadly isn't an option. Yes I though preg_match could do this, but I have no idea on how to formulate it... Hang on and I'll note out what the input and output for that section of the script is, and what the full section is. Just to clear it up. I'll post it all here in roughly 10 or so minutes. Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/#findComment-1050630 Share on other sites More sharing options...
loco41211 Posted April 29, 2010 Author Share Posted April 29, 2010 Here is the full bit for that section: function get_user($link) { $config=fopen($link,'r'); while(!feof($config)) { $line=fgets($config); if (strstr($line,'user') or strstr($line,'username') { if (strrpos($line,'"')) $getuser=substr($line,(strpos($line,'=')+3),(strrpos($line,'"')-(strpos($line,'=')+3))); else $getuser =substr($line,(strpos($line,'=')+3),(strrpos($line,"'")-(strpos($line,'=')+3))); return $getuser; I had a thought, the script thinks the username stops where the quotation mark is. Would there be a way of getting it to check " to " ? In other words, what's in the quotation marks after the variable name is found? Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/#findComment-1050634 Share on other sites More sharing options...
jdavidbakr Posted April 29, 2010 Share Posted April 29, 2010 You have identified the line and you just want to extract the value within the quotes, right? preg_match_all("/\"([^\"]+)\"/", $string, $matches); $value = $matches[1][0]; Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/#findComment-1050638 Share on other sites More sharing options...
loco41211 Posted April 29, 2010 Author Share Posted April 29, 2010 You have identified the line and you just want to extract the value within the quotes, right? preg_match_all("/\"([^\"]+)\"/", $string, $matches); $value = $matches[1][0]; Changing the bits to if (strrpos($line,'"')) $getuser=substr($line,(strpos($line,'"')+1),(strrpos($line,'"')-(strpos($line,'"')+1))); else $getuser=substr($line,(strpos($line,"'")+1),(strrpos($line,"'")-(strpos($line,"'")+1))); return $getuser; Seemed to do the trick! But it looks messy, and I don't know if it will always work as needed so I'm going to try and implement your idea. Thank you, I'll post an update in a bit once I figure out how to implement what you gave me. Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/#findComment-1050641 Share on other sites More sharing options...
loco41211 Posted April 29, 2010 Author Share Posted April 29, 2010 It worked! I used this preg_match_all("/\"([^\"]+)\"/", $line, $matches); $pass = $matches[1][0]; return $pass; One last question, what would the regex be to preg_match the value between apostrophe marks? for example $variable = 'hello'; Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/#findComment-1050648 Share on other sites More sharing options...
jdavidbakr Posted April 29, 2010 Share Posted April 29, 2010 It worked! I used this preg_match_all("/\"([^\"]+)\"/", $line, $matches); $pass = $matches[1][0]; return $pass; One last question, what would the regex be to preg_match the value between apostrophe marks? for example $variable = 'hello'; This should match either single or double quotes. (Note that it will not match single quotes within double quotes and vice versa) preg_match_all("/[\"']([^\"']+)[\"']/", $line, $matches); $pass = $matches[1][0]; return $pass; Inside the []'s it will match any of the characters, inside [^] it will match anything but the characters listed. The []+ will match one or more characters. So this will match either a single or double quote, then any non-zero number of anything except single or double quotes, followed by a single or double quote. The parentheses determine what will be passed to $matches. Technically this will also match a string with a double-quote open and a single-quote close, i.e. 'the string' "the string" "the string' 'the string" but won't match "don't match this" or 'I said, "Do not match this"' Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/#findComment-1050663 Share on other sites More sharing options...
loco41211 Posted April 29, 2010 Author Share Posted April 29, 2010 Your 'hybrid' of both matches didn't match the inbetween apostrophes for some reason. I have come up with this, and it seems to work: preg_match_all("/\"([^\"]+)\"/", $line, $matches); else preg_match_all('/\'([^\"]+)\'/', $line, $matches); $pass = $matches[1][0]; return $pass; Thank you for the help, it's much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/200188-need-a-suggested-improvement-to-the-following-code/#findComment-1050670 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.