Jump to content

Need a suggested improvement to the following code...


loco41211

Recommended Posts

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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';

Link to comment
Share on other sites

 

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"'

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.