S
S
svscorp2012-09-14 17:26:56
PHP
svscorp, 2012-09-14 17:26:56

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

5 answer(s)
U
Urvin, 2012-09-14
@svscorp

#^.*?version=(\d+)$#si
Something like this

A
akral, 2012-09-14
@akral

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+)/

N
NeX, 2012-09-14
@NeX

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"

G
gaelpa, 2012-09-14
@gaelpa

"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]); }

S
svscorp, 2012-09-16
@svscorp

Thanks everyone for the comments and guidance. I will learn materiel.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question