Answer the question
In order to leave comments, you need to log in
Need help pulling a parameter through regular expressions
Good afternoon, Habr.
I'm doing a regex in PHP. Can't figure out one thing.
There is a file:
#Fri, 14 Sep 2012 12:29:26 +0400
#Wed Aug 08 17:40:54 MSD 2012
version=116
, with tabs and stuff, it looks like this: clip2net.com/s/2iEDP
I need to pull the version value out of it. I do it like this:
$version_file = 'version.file';
$version_data = file_get_contents($version_file, FILE_USE_INCLUDE_PATH);
preg_match('/.*version=([0-9]+)\n*/Usmi', $version_data, $matches);
$version = intval($matches[1]);
The problem is that $version is returned = 1. Even though it is 116 in the file.
print_r($matches) outputs:
Array ( [0] => #Fri, 14 Sep 2012 12:29:26 +0400 #Wed Aug 08 17:40:54 MSD 2012 version=1 [1] => 1 )
I'm doing something wrong...
Answer the question
In order to leave comments, you need to log in
I don’t understand why at the beginning and end both the author and the respondents add a full search.
Just look for the part you need:
/version=(\d+)/
preg_match('/.*version=([0-9]+?)\n*/Usmi', $version_data, $matches);
en.wikipedia.org/wiki/%D0%E5%E3%F3%EB%FF%F0%ED%FB%E5_%E2%FB%F0%E0%E6%E5%ED%E8%FF
see Greedy and lazy quantification"
"Bumps" parsing configs in the "standard" format for niks, without cutting off possible quotes.
$lines=explode("\n",file_get_contents('file.conf'));
$params=array();
foreach ($lines as $line) {
$line=trim($line);
if (strpos($line,'#')===0) continue;
$pair=explode('=',$line,2);
$params[trim($pair[0])]=trim($pair[1]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question